Skip to content

Instantly share code, notes, and snippets.

@udhos
Last active November 12, 2021 18:56
Show Gist options
  • Select an option

  • Save udhos/0df16ecc9fd75271f1dee46d43bb7cd2 to your computer and use it in GitHub Desktop.

Select an option

Save udhos/0df16ecc9fd75271f1dee46d43bb7cd2 to your computer and use it in GitHub Desktop.
mongodb_last_inserted.txt
You can run the operation in the web shell below:
https://docs.mongodb.com/manual/tutorial/query-arrays/
# to find(query) with two conditions on the same field, use $and:
db.inventory.find(
{$and:
[
{tags: {$size:3}},
{tags: { $elemMatch: { name: "red" } }}
]
}
)
# find a field with a value in a list
db.person.find( { eq3PersonId : { $exists: true }, eq3PersonId: { "$nin": [ null,"" ] }, products: { $elemMatch: { productType: "PAYMENT_ACCOUNT" } } } ).limit(5)
db.person.find( { personal : { $exists: false }, creationDate: {$gte:ISODate("2021-11-10"),$lt:ISODate("2021-11-11")} } ).limit(5)
db.person.find( { personal : { $exists: false } } ).sort({creationDate: -1}).limit(5)
db.person.find({}).sort({creationDate:-1}).limit(2)
# last inserted:
db.collection.find({}).sort({_id:-1}).limit(1).pretty()
# last inserted without field personId:
db.collection.find({"personId" : {"$exists" : false}}).sort({_id:-1}).limit(1).pretty()
https://www.tutorialspoint.com/mongodb-query-to-get-last-inserted-document
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment