Skip to content

Mustafa Ateş UZUN Blog

mustafauzun.co

  • About
  • Blog
  • Home
  • »
  • MongoDB
  • »
  • Short MongoDB Queries
  • »
    • May 17, 2023
    • by mustafauzun
  • MongoDB

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'}}})
Share

Javascript, Infinite Loops

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Javascript, Task Queue
  • Javascript, Thread Pool
  • Javascript, Lexical Environment
  • Javascript, Execution Context
  • Javascript, Event Delegation

Recent Comments

No comments to show.

Archives

  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • January 2022

Categories

  • C#
  • CMD
  • CSS
  • HTML
  • JavaScript
  • MongoDB
  • Node.js
  • Podcast
  • Powershell
  • Uncategorized
Theme: Puskar by Template Sell.