Short MongoDB Queries
To sort a table in reverse order, you can use the “-1” number in your query.
db.getCollection('Table') .find({}).sort({_id: -1})
If you want to find a value from within a property.
db.getCollection('Table').find({'myRow.otherRowinmyRow': 'Hello World'})
To quickly access the logs of a specific account code.
var account = db.getCollection('Account').findOne({code: 'admin'})
db.getCollection('Table').find({accountId: account._id}).sort({_id: -1})
Provide a list of an account’s entries between two dates.
var account = db.getCollection('Account').findOne({code: 'admin'})
db.getCollection('Table').find({"accountId": account._id, "CreateDate":{$gte:ISODate("2023-05-05"),$lt:ISODate("2023-08-25")}})
Provide the number of documents in an array between two dates.
var account = db.getCollection('Account').findOne({code: 'admin'})
db.getCollection('Table').aggregate([
{ "$match": { "accountId": account._id, "createDate":{$gte:ISODate("2023-05-05"),$lt:ISODate("2023-08-25")} } },
{ "$group": {
"_id": null,
"count": {
"$sum": { "$size": "$Documents" }
}
} }
])
Find by verification code within a document array.
db.getCollection('Table').find({Documents: { $elemMatch : { 'code' : '111'}}})