Using Date in MongoDB query JSON object - json

I have a function that creates a query object for me based on a number of conditions:
compileSearchCriteria(search){
var conditions = {
published: true
};
conditions["publishedDate"] = {$lte: new Date(search.searchDate)};
<more code here>
return conditions;
},
Dates are stored as ISODates in MongoDB. The resulting query looks like this:
{"published":true,"publishedDate":{"$lte":"2016-03-27T16:23:12.000Z"}}
and returns nothing.
db.trips.find({"published":true,"publishedDate":{"$lt":ISODate("2016-03-27T15:48:41.866Z")}})
returns desired results. Sending the query direct works as well:
DealsCollection.find({published:true, publishedDate:{$lt: new Date(this.props.searchDate)}}).fetch()
How can I create the query object so that I get proper results?
Update
It should work and it does work. The problem seems to be with mini-mongo implementation in Meteor. I separated the calls to see where the disconnect is. The server returned data using the original query. But querying resulting documents from the mini-mongo collection produced no results, even though I can see returned documents in it. That is a different issue. Thank you for pointers.

Related

Filter an array passed from query params. NestJS, TypeORM

I’m using NestJS with TypeORM, database is MySQL, and I’d like to filter multiple parameters that can be passed in.
The frontend has a list of products and filters are applied as query params sent to NestJS, filtering works for a single param eg api.example.com?manufacturer=Acer but how would I filter an Array eg api.example.com?manufacturer=Acer,Toshiba,Asus.
I tried quite a few things in TypeORM, currently using the QueryBuilder to build the array with an if statement if the filter exists if so I’m doing something like a where statement.
.andWhere(manufacturer = filterOne, {filterOne: *manufacturers from the query param*})
But yeah just can’t hack something together, tried a couple of things, above is a rough example, did try methods that TypeORM had as an example on filtering arrays but it seemed like it was more for an array of integers only? Regardless, I’m open to any methods that allow for the end result of filtering the example I provided, cheers and thanks again!
You have to use IN to get all data where manufacturer equal the data came from the query, first, you have to convert the query to an array:
var manufacturerParam = filterOne.split(",");
then add it to your query:
.andWhere(manufacturer IN (:filter)", { filter: manufacturerParam })

Avoid some query fields from filtering results

When I make a GET request to a service with a query string as in /restaurants?includeData, by default includeData is used to filter the restaurants giving me zero results.
How can I avoid this specific query field from being used in the filtering so that I can use it for my own purposes in a hook?
You can create another before hook that modifies the hook.params.query and hook object to what you need:
app.service('myservice').before(function(hook) {
hook.includeData = hook.params.query.includeData;
delete hook.params.query.includeData;
});

Is there way to return all data from couchbase view

I wrote a Couchbase view and I'm querying this view from Python. Instead of the full set of data being returned, it is only returning the first 10 elements.
I don't want pagination, instead of pagination I want the entire set of data. Is there any way of doing this?
Below is my view code:
function (doc, meta) {
if (doc.type == "folder" && doc.location) {
emit(doc.location, meta.id);
}
}
And here is the Python code I wrote to run the query:
view_endpoint = "_design/restViews/_view/"+viewName+"?key=%22"+key
response = requests.get(view_url + view_endpoint).json()
return response
A Trusty source says:
When querying the view results, a number of parameters can be used to select, limit, order and otherwise control the execution of the view and the information that is returned. When a view is accessed without specifying any parameters, the view will produce results matching the following: Full view specification, i.e. all documents are potentially output according to the view definition.Limited to 10 items within the Admin Console, unlimited through the REST API.Reduce function used if defined in the view.Items sorted in ascending order (using UTF–8 comparison for strings, natural number order)
You can add parameters to the query before the view. One of them is:
limit: numeric Limit- the number of the returned documents to the specified number.
Good luck. :)

Help with Linq to SQL; return one result from one to many relationship

I am trying to return a list of results, where the joined table has multiple values, but I only want one image value for each listing value.
The SQL is like this:
SELECT
Title, Comments, ThumbNailPath, thumbheight, thumbwidth
FROM
Listings as l INNER JOIN
Images as i ON l.id = i.ListingId
WHERE
(i.ImageSlot = 1)
My repository class looks something like this: (db.GetListings() is the stored procedure method I added to the methods section of the .dbml file)
private ListingModelDataContext db = new ListingModelDataContext();
public IQueryable FindAllListings()
{
//return all listings and first associated thumbnail
return db.GetListings();
}
When I try calling from stored procedure I get error
'System.Data.Linq.ISingleResult' to 'System.Linq.IQueryable'. An
explicit conversion exists (are you
missing a cast?)
I am looking for guidance on how I might either call that sql statement from a stored procedure or simply structure the linq to return that value.
My preference is to know how to write the LINQ statement. To clarify, I have a one to many relationship on the images table. (multiple images per listing) I only want to return the first image though on a search results page. So, for each listing return image value where imageSlot = 1.
I should get a list of rows showing the listing values joined to the image values. I get the correct results in SQL but not sure how to write it in linq or how to call the sproc correctly.
Thanks in advance and sorry if this is hard to read.
Are you trying to do soemthing like :
var result = (stored proc).Single()
or
var result = (stored proc).SingleOrDefault()
?? This would break if there are indeed multiple elements coming back - instead use the .First() or .FirstOrDefault() methods:
var result = (stored proc).FirstOfDefault();
returns the first entry from the stored proc, or null if the stored proc returns no values at all.
Marc

Linq to Sql: Am I doing this right?

I am working on a database access layer project and decided to use Linq to SQL for it. One of the things I wanted to do was provide an API that can take Linq Expressions as arguments to retrieve data. For instance, I wrote this API now that looks like this:
public Result GetResults(System.Linq.Expressions.Expression<Func<Result, bool>> predicate)
{
Result result = db.Results.SingleOrDefault(predicate);
return result;
}
You can then use this API to query the database for a Result row that satisfies certain conditions, for example:
Result result = Provider.GetResults(r => r.ID == 11);
This works very well. I am able to get the one row I want based on my conditions.
Next step was taking this to be able to get multiple objects back from the database.
The way I got it to work was like this:
public List<Result> GetResults(System.Linq.Expressions.Expression<Func<Result, bool>> predicate)
{
List<Result> results = db.Results.Select(r => r).Where(r => r.ID == 11).ToList<Result>();
return results;
}
As you can see, I call a Select with r => r, this gives me back everything and then I use a Where to filter to what I need.
It works... but something tells me that I am doing it really ugly. I could be wrong, but doesn't this pull EVERYTHING out of the Results table then filters it? or does it put together the correct SQL statement that does filter at the database level?
Anyway... I would highly appreciate some guidance on how I can accomplish this task. How do I write an API that takes a Linq Expression as an argument and returns a set of objects from the database based on that expression.
Thanks!
The Select(r=>r) does nothing (except change from Table<T> to IQueryable<T> - but nothing useful). And I assume you intended to pass predicate to the Where?
Indeed, this doesn't pull everything out and filter it - the appropriate WHERE (TSQL) clause is generated. This is possible because of "deferred execution" and "composability" - meaning: it doesn't actually execute anything until you start iterating the data (in the ToList()) - until then you are simply shaping the query.
You can see this by doing somthing like:
db.Log = Console.Out;
and look at the TSQL. Or run a TSQL trace. To make it prettier, simplify it to:
return db.Results.Where(predicate).ToList();
I know you said you wanted to pass a predicate and return a List but have you considered returning an IQueryable
Then you could call:
GetResults().Where(r => r.SomeProperty == "SomeValue")
.OrderBy(r => r.SomeOtherProperty)
.Skip(10)
.Take(10); //etc.
With your current API design you would return all records and then have to get 10, where as the above would return only the 10 you needed...
Just some thoughts...