Database Articles

Page 235 of 546

Using count equivalent in MongoDB to find top users with max occurrence

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 509 Views

To get the count and top users, use $group along with aggregate() . Let us create a collection with documents −> db.demo264.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47ed441627c0c63e7dba9e") } > db.demo264.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47ed471627c0c63e7dba9f") } > db.demo264.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47ed491627c0c63e7dbaa0") } > db.demo264.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47ed4c1627c0c63e7dbaa1") } > db.demo264.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47ed4e1627c0c63e7dbaa2") } > db.demo264.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47ed531627c0c63e7dbaa3") }Display all documents from a collection with the help ...

Read More

MongoDB query to skip documents

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 245 Views

To skip documents in MongoDB, use skip(). Let us create a collection with documents −> db.demo263.insertOne({_id:100}); { "acknowledged" : true, "insertedId" : 100 } > db.demo263.insertOne({_id:200}); { "acknowledged" : true, "insertedId" : 200 } > db.demo263.insertOne({_id:300}); { "acknowledged" : true, "insertedId" : 300 }Display all documents from a collection with the help of find() method −> db.demo263.find();This will produce the following output −{ "_id" : 100 } { "_id" : 200 } { "_id" : 300 }Following is the query to skip document −> result = db.demo263.aggregate([ ...   { ...      $project: { ...         v_id: { $ifNull: [null, [100, 200]] } ... ...      } ...   }, ...   { $unwind: '$v_id' }, ...   { $sort: { v_id: 1, _id: 1 } }, ... ...   { $skip: 2 }, ...   { $limit: 2 } ...]);This will produce the following output −{ "_id" : 300, "v_id" : 100 } { "_id" : 100, "v_id" : 200 }

Read More

Invert Result of MongoDB Query (Implement Opposite of $and operation)?

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 291 Views

To invert result i.e. opposite of $and operation, use $OR along with $ne. Let us first create a collection with documents −> db.demo4.insert({uid:1, "Name":"Chris", "Age":22}); WriteResult({ "nInserted" : 1 }) > db.demo4.insert({uid:2, "Name":"David", "Age":21}); WriteResult({ "nInserted" : 1 }) > db.demo4.insert({uid:3, "Name":"Bob", "Age":23}); WriteResult({ "nInserted" : 1 }) > db.demo4.insert({uid:1, "Name":"Carol", "Age":20}); WriteResult({ "nInserted" : 1 })Following is the query to display all documents from a collection with the help of find() method −> db.demo4.find();This will produce the following output −{ "_id" : ObjectId("5e0a1da125ddae1f53b62221"), "uid" : 1, "Name" : "Chris", "Age" : 22 } { "_id" : ObjectId("5e0a1db025ddae1f53b62222"), "uid" : ...

Read More

Modify sequence in MongoDB

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 330 Views

To modify a sequence, use findAndModify(). Let us create a collection with documents −> db.demo261.insertOne({_id:100, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 100 }Display all documents from a collection with the help of find() method −> db.demo261.find();This will produce the following output −{ "_id" : 100, "Name" : "Chris" }Following is the query to modify sequence −> db.demo262.insert({_id:"newId", sequence_value:0}) WriteResult({ "nInserted" : 1 }) > function getNext(sName){ ... ...   var d= db.demo262.findAndModify({ ...      query:{_id: sName}, ...      update: {$inc:{sequence_value:1}}, ...      new:true ...   }); ...   return d.sequence_value; ...}Following is the query to call ...

Read More

Invoke convertToCapped and convert an existing collection to capped in MongoDB

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 262 Views

To convert an existing collection to capped, use convertToCapped. Let us create a collection with documents −> db.demo260.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47b1181627c0c63e7dba9b") } > db.demo260.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47b11c1627c0c63e7dba9c") } > db.demo260.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47b11f1627c0c63e7dba9d") }Display all documents from a collection with the help of find() method −> db.demo260.find();This will produce the following output −{ "_id" : ObjectId("5e47b1181627c0c63e7dba9b"), "Name" : "Chris" } { "_id" : ObjectId("5e47b11c1627c0c63e7dba9c"), "Name" : "Bob" } { "_id" : ObjectId("5e47b11f1627c0c63e7dba9d"), "Name" : "David" }Following is the query to invoke convertToCapped ...

Read More

Get MongoDB documents that contains specific attributes in array

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 557 Views

For this, you can use $and along with dot(.) notation. Let us first create a collection with documents −>db.demo2.insertOne({"StudentInformation":[{"StudentName":"John", "StudentAge":21}, {"StudentName":"Mike", "StudentAge":22}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e08b56e25ddae1f53b62219") } >db.demo2.insertOne({"StudentInformation":[{"StudentName":"Carol", "StudentAge":19}, {"StudentName":"Bob", "StudentAge":18}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e08b58625ddae1f53b6221a") }Following is the query to display all documents from a collection with the help of find() method −> db.demo2.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e08b56e25ddae1f53b62219"),    "StudentInformation" : [       {          "StudentName" : "John",          "StudentAge" : 21       }, ...

Read More

How to run MongoDB shell using mongos command?\\n

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 813 Views

In order to launch the MongoDB shell, you need to use mongo command. Following is the syntax −>mongoFirst reach the MongoDB bin directory from command prompt as in the below screenshot −Here is the command to launch the mongo shell as in the below screenshot −This will produce the following output −

Read More

How to update % printed to Console from MongoDB?

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 156 Views

To update and print to console from MongoDB script, create a variable and then use the print() method.Let us first create a variable −> var amount=10.58945;Here is the query to update % printed to console −> var amount=10.58945; > print(amount.toFixed(2)+" %");This will produce the following output −10.59 %

Read More

MongoDB inverse of query to return all items except specific documents?

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 600 Views

To get documents except some specific documents, use $nor along with $and. Let us first create a collection with documents −> db.demo1.insertOne({"StudentName":"Chris", "StudentMarks":38}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e08a4f025ddae1f53b62216") } > db.demo1.insertOne({"StudentName":"David", "StudentMarks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e08a4f725ddae1f53b62217") } > db.demo1.insertOne({"StudentName":"Mike", "StudentMarks":96}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e08a4fd25ddae1f53b62218") }Following is the query to display all documents from a collection with the help of find() method −> db.demo1.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e08a4f025ddae1f53b62216"),    "StudentName" : "Chris",    "StudentMarks" : 38 } {    "_id" : ...

Read More

How to specify the order in which the query returns matching documents in MongoDB

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 131 Views

To specify the order in which the query returns matching documents, use cursor.sort() in MongoDB. The cursor is db.collectionName.find().Let us create a collection with documents −> db.demo259.insertOne({"Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47ae1f1627c0c63e7dba98") } > db.demo259.insertOne({"Subject":"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47ae231627c0c63e7dba99") } > db.demo259.insertOne({"Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47ae281627c0c63e7dba9a") }Display all documents from a collection with the help of find() method −> db.demo259.find();This will produce the following output −{ "_id" : ObjectId("5e47ae1f1627c0c63e7dba98"), "Subject" : "MySQL" } { "_id" : ObjectId("5e47ae231627c0c63e7dba99"), "Subject" : "Java" } { "_id" : ObjectId("5e47ae281627c0c63e7dba9a"), "Subject" ...

Read More
Showing 2341–2350 of 5,456 articles
« Prev 1 233 234 235 236 237 546 Next »
Advertisements