Yes, use getSiblingDB(). Let us add some documents to the database −> use customer_tracker-990; switched to db customer_tracker-990 > db.demo1.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea4697ca7e81adc6a0b3954") } > db.demo1.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea46980a7e81adc6a0b3955") } > db.demo1.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea46984a7e81adc6a0b3956") }Display all documents from a collection with the help of find() method −> db.getSiblingDB("customer_tracker-990").demo1.find();This will produce the following output −{ "_id" : ObjectId("5ea4697ca7e81adc6a0b3954"), "Name" : "Chris" } { "_id" : ObjectId("5ea46980a7e81adc6a0b3955"), "Name" : "David" } { "_id" : ObjectId("5ea46984a7e81adc6a0b3956"), "Name" : "Bob" }Read More
To limit the number of documents in a collection, set capped − true. Set the size there itself. Let us create a collection with documents −> db.createCollection( "demo683", { capped: true, size: 5 ,max:4} ) { "ok" : 1 } > db.demo683.insertOne({Value:100}); { "acknowledged" : true, "insertedId" : ObjectId("5ea468afa7e81adc6a0b394e") } > db.demo683.insertOne({Value:500}); { "acknowledged" : true, "insertedId" : ObjectId("5ea468b0a7e81adc6a0b394f") } > db.demo683.insertOne({Value:1000}); { "acknowledged" : true, "insertedId" : ObjectId("5ea468b1a7e81adc6a0b3950") } > db.demo683.insertOne({Value:400}); { "acknowledged" : true, "insertedId" : ObjectId("5ea468b2a7e81adc6a0b3951") } > db.demo683.insertOne({Value:800}); { "acknowledged" : true, "insertedId" : ObjectId("5ea468b3a7e81adc6a0b3952") } ... Read More
Yes, you can skip some documents using skip() in MongoDB. Use limit() to display how many documents you want to display after skipping some. Let us create a collection with documents −> db.demo682.insertOne({FirstName:"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462a804263e90dac94402") } > db.demo682.insertOne({FirstName:"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462ac04263e90dac94403") } > db.demo682.insertOne({FirstName:"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462af04263e90dac94404") } > db.demo682.insertOne({FirstName:"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462b304263e90dac94405") } > db.demo682.insertOne({FirstName:"Adam"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462ba04263e90dac94406") } > db.demo682.insertOne({FirstName:"Chris"}); { "acknowledged" : true, "insertedId" ... Read More
To get the topmost document, use find() along with limit(). To fetch only a single document, consider using limit(1). Let us create a collection with documents −> db.demo681.insertOne({_id:101, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo681.insertOne({_id:102, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo681.insertOne({_id:103, Name:"David"}); { "acknowledged" : true, "insertedId" : 103 } > db.demo681.insertOne({_id:104, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 104 } > db.demo681.insertOne({_id:105, Name:"Sam"}); { "acknowledged" : true, "insertedId" : 105 }Display all documents from a collection with the help of find() method −> db.demo681.find();This will produce the following output −{ ... Read More
The $and performs a logical AND operation on an array of one or more expressions. Let us create a collection with documents −> db.demo680.insertOne({Values:40}); { "acknowledged" : true, "insertedId" : ObjectId("5ea4461b04263e90dac943fe") } > db.demo680.insertOne({Values:70}); { "acknowledged" : true, "insertedId" : ObjectId("5ea4461e04263e90dac943ff") } > db.demo680.insertOne({Values:[80, 30]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea4462a04263e90dac94400") } > db.demo680.insertOne({Values:20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea4463304263e90dac94401") }Display all documents from a collection with the help of find() method −> db.demo680.find();This will produce the following output −{ "_id" : ObjectId("5ea4461b04263e90dac943fe"), "Values" : 40 } { "_id" : ... Read More
Let us create a collection with documents −> db.demo677.insertOne({Value:10}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421f404263e90dac943f8") } > db.demo677.insertOne({Value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421f704263e90dac943f9") } > db.demo677.insertOne({Value:20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421fa04263e90dac943fa") } > db.demo677.insertOne({Value:20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421fe04263e90dac943fb") }Display all documents from a collection with the help of find() method −> db.demo677.find();This will produce the following output −{ "_id" : ObjectId("5ea421f404263e90dac943f8"), "Value" : 10 } { "_id" : ObjectId("5ea421f704263e90dac943f9"), "Value" : 50 } { "_id" : ObjectId("5ea421fa04263e90dac943fa"), "Value" : 20 } { "_id" : ... Read More
For group query, use MongoDB $group and get the count with $sum. Let us create a collection with documents −> db.demo676.insertOne({"Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41eed04263e90dac943f2") } > db.demo676.insertOne({"Marks":75}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef304263e90dac943f3") } > db.demo676.insertOne({"Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef404263e90dac943f4") } > db.demo676.insertOne({"Marks":65}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef704263e90dac943f5") } > db.demo676.insertOne({"Marks":65}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef804263e90dac943f6") }Display all documents from a collection with the help of find() method −> db.demo676.find();This will produce the following output −{ "_id" : ... Read More
Let us create a collection with documents −> db.demo675.insertOne({ ... "ListOfNames":["John", "Chris", "David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5b404263e90dac943ef") } > db.demo675.insertOne({ "ListOfNames":["Bob", "Johnson", "Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5c804263e90dac943f0") } > db.demo675.insertOne({ "ListOfNames":["Jace", "Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5d304263e90dac943f1") }Display all documents from a collection with the help of find() method −> db.demo675.find();This will produce the following output −{ "_id" : ObjectId("5ea3f5b404263e90dac943ef"), "ListOfNames" : [ "John", "Chris", "David" ] } { "_id" : ObjectId("5ea3f5c804263e90dac943f0"), "ListOfNames" : [ "Bob", "Johnson", "Sam" ] } { "_id" : ObjectId("5ea3f5d304263e90dac943f1"), "ListOfNames" : ... Read More
The $or operator performs a logical OR operation on an array of two or more expressions. Let us create a collection with documents −> db.demo674.insertOne({Name:"Chris", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f33604263e90dac943eb") } > db.demo674.insertOne({Name:"David", Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f33c04263e90dac943ec") } > db.demo674.insertOne({Name:"Bob", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f34204263e90dac943ed") } > db.demo674.insertOne({Name:"John", Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f34804263e90dac943ee") }Display all documents from a collection with the help of find() method −> db.demo674.find();This will produce the following output −{ "_id" : ObjectId("5ea3f33604263e90dac943eb"), "Name" : "Chris", ... Read More
Use max field in order to limit the number of documents in the collection. Following is the query to use the max field in the capped collection −> db.createCollection("demo673", { capped : true, size : 100, max :50 } ) { "ok" : 1 }Let us create a collection with documents −> db.demo673.insertOne({Name:"John", Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7304263e90dac943e8") } > db.demo673.insertOne({Name:"Bob", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7804263e90dac943e9") } > db.demo673.insertOne({Name:"David", Age:20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7f04263e90dac943ea") }Display all documents from a collection with the help of find() ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP