I am trying to post a query to Sails.js via HTTP POST in order to find records created after a certain point in time.
Using the following syntax works perfectly and gives me back one record with the correct time:
localhost:1337/object?where={"createdAt":"2014-08-19T14:36:16.047Z"}
When trying to add an operator the query does not work any longer giving me no records in the object collection :
localhost:1337/object?where={"createdAt":{">":"2014-08-19T14:36:16.047Z"}}
Due to the fact that the date conversion from ISODate to dateTime works for the exact query I suppose it should also work for the ">" query.
Highly appreciate some help on this one.
Related
I'm trying to create a query that can pull this information from my database using just the type score when it's less than 70 and when it's of type exam:
{"_id":0,"name":"aimee Zank",
"scores":[{"score":1.463179736705023,"type":"exam"},
{"score":11.78273309957772,"type":"quiz"},{"score":35.8740349954354,"type":"homework"}]}
I'm not getting any errors and it's turning up blank. Here's the query I'm trying to use:
db.students.find({"scores":[{"score":{$lt:70},"type":"exam"}]}).pretty
Any help would be appreciated :)
That is not how you match elements inside array. Use $elemMatch :
db.students.find({"scores":{$elemMatch:{score:{$lt:70},type:"exam"}}})
Update:
OP marked the post as "not answered" and then come with twisted requirement. Going forward, make sure you post a seperate question for your entirely new requirements or state your problem properly, dont extend it as the time flies. However, here's the filter query using aggregation framework:
db.collection.aggregate([
{$addFields:{scores:{$filter:{
input:"$scores",
as:"score",
cond:{$and:[{$eq:["$$score.type","exam"]},{$lt:["$$score.score",70]}]}
}}}}
])
This filters all exams that have score less than 70.
I have a Firestore database which contains a list of jobs in a "jobs" collection. Each job has an end date which is a "timestamp" data type. When I try to query using a where condition, I get no data back when using the react-native-firebase module.
Snapshot of data
Below is the code I'm using:
return db.collection('jobs')
.where('end', '>=', new Date());
Also tried:
.where('end', '>=', new Date().toISOString());
Is there a specific data type/format I must send the date value as for it to match?
When I use the standard web SDK from Firebase on my web app, the data comes back correctly.
We had an issue with date conversion going across the React Native bridge, we've since fixed this and the fix will be part of the v3.1.0 release in the next day or so.
An example of date querying can be seen in our tests: https://github.com/invertase/react-native-firebase/blob/master/tests/src/tests/firestore/collectionReferenceTests.js#L523
So you are querying correctly with .where('end', '>=', new Date());
Thanks
I am trying to query dates in mongodb.
The dates are stored as ISODate("2015-10-08T05:48:55.778+0000").
Now how should i do query like $gte or $lte.
I have been using Play plugin for reactive mongo
To query from the mongo shell, I would need to query with=>
{"endDateTime":{"$eq": new Date("2017-10-08T05:48:55.778+0000")}
OR,
{"endDateTime":{"$eq": ISODate("2017-10-08T05:48:55.778+0000")}
So, what should I do to query it using play reactive mongo. I have been using JodaTime. I am generating the Json Object of the query, and feeding to the find() api straightaway.
*Yes there a lot suggestion in SO, about the topic, but none of them seem to help me in this case. I could give more info if needed.
Update Answer:
Seems like I had some confusion, when converting the dates.
When I tried converting the String Date to Joda DateTime , the result when I print it in console, it would be shown as timestamp,but when I sent it to reactive mongo find it would convert to some form of string date "2015-10-08T05:48:55.778+0000".
So, I had to retrieve the millisecond conversion and send it to the respective api, and mongo would process without any issues.
I have a collection of data and I want to get it sorted by insertion time. I have not any additional fields to store the insert time. But as I found out I can get this time from Id.
I have tried this code:
return bookmarks.find({}, {sort: {_id.getTimestamp(): 1}, limit: 10});
or
return bookmarks.find({}, {sort: {ObjectId(_id).getTimestamp(): 1}, limit: 10});
but get the error message:
=> Your application has errors. Waiting for file change.
Is there any way to sort collection by insertion datetime using only id field ?
At the moment this isn't possible with Meteor, even if it is with MongoDB. The ObjectID's created with meteor don't bear a timestamp. See http://docs.meteor.com/#collection_object_id
The reason for this is client side code can insert code and it can arrive late on the server, hence there is no guarantee the timestamp portion of the ObjectID will be accurate. In addition to the latency the client side's date is used meaning if they're off it's going to get you incorrect data. I think this is the reason they use an ObjectID but it is completely random.
If you want to sort by date you have to store the time/date separately.
The part what i striked out is not accurate. Meteor use it is own id generation which is based on a random string that is while does not apply the doc what i linked before. Check sasha.sochka's comment under.
It is nearly but not 100% good if you just sort for the _id field . While as it is constructed the first 4 byte is the timestamp in secs (so sorting for the getTimestamps value is not better). Under one second resolution you cannot get the exact order, as it is mentioned in the documentation: http://docs.mongodb.org/manual/reference/object-id/#objectid
It is still true that you can try to check the exact order of the insert/update ops against your collection in the oplog, if you have an oplog, but as it is a capped collection anyway you will see the recent operations only. http://docs.mongodb.org/manual/core/replica-set-oplog/.
I'm writing a long-polling script to check for new documents in my mongo collection and the only way I know of checking to see whether or not changes have been made in the past iteration of my loop is to do a query getting the last document's ID and parsing out the timestamp in that ID and seeing if it's greater than the timestamp left since I last made a request.
Is there some kind of approach that doesn't involve making a query every time or something that makes that query the fastest?
I was thinking a query like this:
db.chat.find().sort({_id:-1}).limit(1);
But it would be using the PHP driver.
The fastest way will be creating indexes on timestamp field.
Creating index:
db.posts.ensureIndex( { timestamp : 1 } )
Optimizes this query:
db.posts.find().sort( { timestamp : -1 } )
findOne give you only one the last timestamp.
nice to help you.
#Zagorulkin Your suggestion is surely going to help in the required scenario. However i don't think so sort() works with findOne().