MongoDB is an open source NoSQL database. It stores or retrieve data in the form of JSON-like documents. Just like Mysql where we can select records with specific conditions, ordering records, grouping records, limiting records, using Math functions etc. Similarly, In mongodb we can find records with conditions, limiting records etc. In this tutorial we will learn how to query in MongoDB.
db.users.find({“name”: /.m./})
This will return documents where name contains ‘m’.
db.users.find( { age: { $gt: 12 } } )
This will return documents where age greater than 12.
db.users.find( { _id: 5 } )
This will return documents where id is 5.
db.users.find( { birth: { $gt: new Date(‘1940-01-01’), $lt: new Date(‘1960-01-01’) } } )
This operation returns from the documents where birth is between new Date(‘1940-01-01’) and new Date(‘1960-01-01’).
db.users.find( {
birth: { $gt: new Date(‘1920-01-01’) },
death: { $exists: false }
} )
This operation returns all the documents where birth field is greater than new Date(‘1950-01-01’) and death field does not exists.
db. users.find(
{ awards: { $elemMatch: { award: “Turing Award”, year: { $gt: 1980 } } } }
)
This operation returns documents where the awards array contains at least one element with both the award field equals “Turing Award” and the year field greater than 1980.
db.users.find( { }, { name: 1, contribs: 1 } )
This operation finds all documents and returns only the name field, contribs field and _id field.
db.users.find().limit( 5 )
This will return only 5 documents.
db.users.find().skip( 5 )
This will skip or offset 5 documents from start.
db.users.find().count()
This will count the documents.
db.users.find().sort( { name: 1 } )
This operation returns documents sorted in ascending order by the name field.
These are the basic mongodb queries you can use in your project. In detail you can about mongodb query here.
How to validate form in ReactJS
How to create charts in ReactJS
For more Mongodb Tutorials Click here, Node.js Tutorials Click here.