Kamilski81
Kamilski81

Reputation: 15117

Mongo - Retain ordering based on the query params?

How could I do the following in mongo?

db.questions.find( { _id: { $in: [ "506b422ff42c95000e000004", "506b422ff42c95000e00007a", "506b422ff42c95000e00008d"] } } ).sort( {_id: { $in: [ "506b422ff42c95000e00007a", "506b422ff42c95000e00008d", "506b422ff42c95000e000004"]}});

Is it even possible or do I have to do this on the code end?

Upvotes: 0

Views: 191

Answers (1)

cirrus
cirrus

Reputation: 5672

No, not really. You'll need to do that yourself. The results won't come back in the order you construct your query. See this similar question: Order of responses to MongoDB $in query?

Your ID's do look numerically ordered though and they're based on date, so you could sort on _id itself, but there's no way of even determining if those objects would even be stored in that order in the database.

Upvotes: 1

Related Questions