Find objects between two dates MongoDB

Querying for a Date Range (Specific Month or Day) in the MongoDB Cookbook has a very good explanation on the matter, but below is something I tried out myself and it seems to work. items.save({ name: “example”, created_at: ISODate(“2010-04-30T00:00:00.000Z”) }) items.find({ created_at: { $gte: ISODate(“2010-04-29T00:00:00.000Z”), $lt: ISODate(“2010-05-01T00:00:00.000Z”) } }) => { “_id” : ObjectId(“4c0791e2b9ec877893f3363b”), “name” … Read more

How do I perform the SQL Join equivalent in MongoDB?

As of Mongo 3.2 the answers to this question are mostly no longer correct. The new $lookup operator added to the aggregation pipeline is essentially identical to a left outer join: https://docs.mongodb.org/master/reference/operator/aggregation/lookup/#pipe._S_lookup From the docs: { $lookup: { from: <collection to join>, localField: <field from the input documents>, foreignField: <field from the documents of the … Read more

Retrieve only the queried element in an object array in MongoDB collection

MongoDB 2.2’s new $elemMatch projection operator provides another way to alter the returned document to contain only the first matched shapes element: db.test.find( {“shapes.color”: “red”}, {_id: 0, shapes: {$elemMatch: {color: “red”}}}); Returns: {“shapes” : [{“shape”: “circle”, “color”: “red”}]} In 2.2 you can also do this using the $ projection operator, where the $ in a … Read more