Calculate skip value for given record for sorted paging

This is called “forward paging” which is a concept you can use to “efficiently page” through results in a “forward” direction when using “sorted” results. JavaScript logic included (because it works in the shell), but not hard to translate. The concept in general: { “_id”: 1, “a”: 3 }, { “_id”: 2, “a”: 3 }, … Read more

Mongodb query specific month|year not date

With MongoDB 3.6 and newer, you can use the $expr operator in your find() query. This allows you to build query expressions that compare fields from the same document in a $match stage. db.customer.find({ “$expr”: { “$eq”: [{ “$month”: “$bday” }, 9] } }) For other MongoDB versions, consider running an aggregation pipeline that uses … Read more

Mongodb $push in nested array

Probably something like this where ID is your ObjectId. The first {} are necessary to identify your document. It is not required to use an ObjectId as long as you have another unique identifier in your collection. db.collection.update( { “_id”: ID, “playlists._id”: “58”}, { “$push”: {“playlists.$.musics”: { “name”: “test name”, “duration”: “4.00” } } } … Read more