Convert aggregation query to find

75 views
Skip to first unread message

Robert James Liguori

unread,
Feb 8, 2017, 10:12:49 AM2/8/17
to mongodb-user
How do I convert this aggregation to a find?  Thanks.

pipeline = [{"$match": {"$and": [{"_id": {"$regex": '^' + nameAndNumber}},
{"times.milliseconds": {"$gte": millisecondsStartRange}},
{"times.milliseconds": {"$lte": millisecondsEndRange}}]}}]

fdm_data = list(db.parameters.aggregate(pipeline))

Rhys Campbell

unread,
Feb 8, 2017, 10:27:45 AM2/8/17
to mongodb-user
db.parameters.find({"$and": [{"_id": {"$regex": '^' + nameAndNumber}}, 
                {"times.milliseconds": {"$gte": millisecondsStartRange}}, 
{"times.milliseconds": {"$lte": millisecondsEndRange}}]});

Robert James Liguori

unread,
Feb 8, 2017, 11:00:46 AM2/8/17
to mongodb-user
Thanks Rhys!!! Yeah... i actually figure it out before you told me... so I'm on the same page!  But the there was no big difference in speed between the find and the aggregation.

I'm now, trying to pull the times out of the id, versus the times.milliseconds field... can't figure out how to slice and dice it.  Can you help?  [ assuming this may speed things up ]

That is...

_id: name:number:milliseconds

fdm_data = list(db.parameters.find({"$and": [{"_id": {"$regex": '^' + nameAndNumber}},
                                      #REPLACE#       {"times.milliseconds": {"$gte": millisecondsStartRange,
                                             {"[AFTER SECOND COLON IN _ID]": {"$gte": millisecondsStartRange,
"$lte": millisecondsEndRange}}]}))

Robert James Liguori

unread,
Feb 8, 2017, 12:30:47 PM2/8/17
to mongodb-user
here is the best refactoring I think I can do...

fdm_data = list(db.parameters.find({"_id": {"$gte": nameAndNumber + str(millisecondsStartRange),
"$lte": nameAndNumber + str(millisecondsEndRange)}}))

Rhys Campbell

unread,
Feb 9, 2017, 9:01:29 AM2/9/17
to mongodb-user
If you want to slice up the id you can use $split


This means going back to the aggregation framework. I'm not sure you're get any significant speed improvments out it this way though.


If you're having performance problems show us the output of...

db.parameters.({"_id": {"$gte": nameAndNumber + str(millisecondsStartRange), "$lte": nameAndNumber + str(millisecondsEndRange)}})).explain()

and

db.parameters.getIndexes()

A sample document or two would be handy.




Rhys Campbell

unread,
Feb 9, 2017, 9:04:28 AM2/9/17
to mongodb-user
explain is probably better executed with "executionStats" verbosity

Asya Kamsky

unread,
Feb 13, 2017, 9:29:24 AM2/13/17
to mongod...@googlegroups.com
This is exactly the most efficient find operation you can run. 

Asya

--


You received this message because you are subscribed to the Google Groups "mongodb-user"


group.


 


For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/


---


You received this message because you are subscribed to the Google Groups "mongodb-user" group.


To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.


To post to this group, send email to mongod...@googlegroups.com.


Visit this group at https://groups.google.com/group/mongodb-user.


To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/fcc32591-d2f4-47d9-aa49-0d80ea89ec22%40googlegroups.com.


For more options, visit https://groups.google.com/d/optout.


Reply all
Reply to author
Forward
0 new messages