AmitDiwan has Published 10744 Articles

Limit the number of documents in a collection in MongoDB?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 10:24:09

457 Views

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" : ... Read More

Is there any way to skip some documents in MongoDB?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 10:22:17

163 Views

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, ... Read More

Get the top most document from a MongoDB collection

AmitDiwan

AmitDiwan

Updated on 13-May-2020 10:19:35

191 Views

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, ... Read More

Set $gt condition in MongoDB $and

AmitDiwan

AmitDiwan

Updated on 13-May-2020 10:18:33

227 Views

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]}); { ... Read More

MongoDB query to add up the values of a specific field in documents

AmitDiwan

AmitDiwan

Updated on 13-May-2020 10:09:06

272 Views

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, ... Read More

MongoDB Group query to get the count of repeated marks in documents?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 10:06:36

201 Views

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, ... Read More

Set regex in MongoDB $in?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 10:03:57

151 Views

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" : ... Read More

MongoDB to fetch documents with $or Operator

AmitDiwan

AmitDiwan

Updated on 13-May-2020 10:02:06

192 Views

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") } > ... Read More

What does the max field mean in the output of MongoDB db..stats( )?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 10:00:27

97 Views

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 ... Read More

Fetch a specific document in MongoDB with array elements

AmitDiwan

AmitDiwan

Updated on 13-May-2020 09:58:05

224 Views

To fetch a specific document, use dot notation in MongoDB find(). Let us create a collection with documents −> db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"Oppo"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3ea9b04263e90dac943e5") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"Samsung"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3eaa404263e90dac943e6") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"OnePlus"}]}); {    "acknowledged" ... Read More

Advertisements