Found 1349 Articles for MongoDB

How to pull all elements from an array in MongoDB without any condition?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

281 Views

You can use $set operator for this. Let us first create a collection with documents −> db.pullAllElementDemo.insertOne( ...    { ...       "StudentId":101, ...       "StudentDetails" : [ ...          { ... ...             "StudentName": "Carol", ...             "StudentAge":21, ...             "StudentCountryName":"US" ...          }, ...          { ...             "StudentName": "Chris", ...             "StudentAge":24, ...             ... Read More

Update object at specific Array Index in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

517 Views

To update object at specific array index, use update() in MongoDB. Let us first create a collection with documents −> db.updateObjectDemo.insertOne( ...   { ...       id : 101, ...       "StudentDetails": ...       [ ...          [ ...             { ...                "StudentName": "John" ...             }, ...             { "StudentName": "Chris" } ...          ], ...          [ { "StudentName": "Carol" }, ... Read More

How to increment a field in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

293 Views

To increment a field in MongoDB, you can use $inc operator. Let us first create a collection with documents −> db.incrementDemo.insertOne({"PlayerScore":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc81cdd8f9e6ff3eb0ce44e") } > db.incrementDemo.insertOne({"PlayerScore":290}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc81ce28f9e6ff3eb0ce44f") } > db.incrementDemo.insertOne({"PlayerScore":560}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc81ce68f9e6ff3eb0ce450") }Following is the query to display all documents from a collection with the help of find() method −> db.incrementDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cc81cdd8f9e6ff3eb0ce44e"), "PlayerScore" : 100 } { "_id" : ObjectId("5cc81ce28f9e6ff3eb0ce44f"), "PlayerScore" : 290 } { "_id" : ObjectId("5cc81ce68f9e6ff3eb0ce450"), "PlayerScore" : ... Read More

What is the syntax for boolean values in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

5K+ Views

You can use $eq as well as $ne operator for boolean values. Let us first create a collection with documents −> db.booleanQueryDemo.insertOne({"UserName":"John", "UserAge":23, "isMarried":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc815c08f9e6ff3eb0ce44a") } > db.booleanQueryDemo.insertOne({"UserName":"Chris", "UserAge":21, "isMarried":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc815d08f9e6ff3eb0ce44b") } > db.booleanQueryDemo.insertOne({"UserName":"Robert", "UserAge":24, "isMarried":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc815dc8f9e6ff3eb0ce44c") } > db.booleanQueryDemo.insertOne({"UserName":"David", "UserAge":26, "isMarried":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc815ed8f9e6ff3eb0ce44d") }Following is the query to display all documents from a collection with the help of find() method −> db.booleanQueryDemo.find().pretty();This will produce the following output −{ ... Read More

How to concatenate results in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

239 Views

You can concatenate results with the help of forEach(). Let us first create a collection with documents −> db.concatenateDemo.insertOne({"Name":"John", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc80dd88f9e6ff3eb0ce448") } > db.concatenateDemo.insertOne({"Name":"Carol", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc80de18f9e6ff3eb0ce449") }Following is the query to display all documents from a collection with the help of find() method −> db.concatenateDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc80dd88f9e6ff3eb0ce448"),    "Name" : "John",    "Age" : 21 } {    "_id" : ObjectId("5cc80de18f9e6ff3eb0ce449"),    "Name" : "Carol",    "Age" : 23 }Here is the query to concatenate results ... Read More

Remove and update the existing record in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

108 Views

You can use only $pull operator that removes and updates the existing record in MongoDB. Let us first create a collection with documents −> db.removeDemo.insertOne( ...    { ...       "UserName" : "Larry", ...       "UserDetails" : [ ...          { ...             "_id" : 101, ...             "UserEmailId" : "976Larry@gmail.com", ...          } ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7f9f88f9e6ff3eb0ce446") } > db.removeDemo.insertOne( ...    { ... ... Read More

Iterate a cursor and print a document in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

220 Views

For this, use printjson. Let us first create a collection with documents −> db.cursorDemo.insertOne({"StudentFullName":"John Smith", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7f0d08f9e6ff3eb0ce442") } > db.cursorDemo.insertOne({"StudentFullName":"John Doe", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7f0df8f9e6ff3eb0ce443") } > db.cursorDemo.insertOne({"StudentFullName":"Carol Taylor", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444") } > db.cursorDemo.insertOne({"StudentFullName":"Chris Brown", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7f0f88f9e6ff3eb0ce445") }Following is the query to display all documents from a collection with the help of find() method −> db.cursorDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc7f0d08f9e6ff3eb0ce442"),    "StudentFullName" : "John ... Read More

Find all collections in MongoDB with specific field?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

881 Views

Let us implement the above syntax in order to find all documents in MongoDB with field name “StudentFirstName”. The query is as follows −> db.getCollectionNames().forEach(function(myCollectionName) { ...    var frequency = db[myCollectionName].find({"StudentFirstName": {$exists: true}}).count(); ...    if (frequency > 0) { ...       print(myCollectionName); ...    } ... });This will produce the following output −multiDimensionalArrayProjection removeKeyFieldsDemo stringOrIntegerQueryDemoLet us verify the removeKeyFieldsDemo collection has field with name “StudentFirstName” or not. Following is the query −> db.removeKeyFieldsDemo.find({"StudentFirstName":{$exists:true}});This will produce the following output displaying the StudentFirstName field exist −{ "_id" : ObjectId("5cc6c8289cb58ca2b005e672"), "StudentFirstName" : "John", "StudentLastName" : "Doe" } { "_id" ... Read More

Pull multiple objects from an array in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

876 Views

To pull multiple objects from an array, you can use $pull operator. Let us first create a collection with documents −> db.pullMultipleObjectsDemo.insertOne( ...    { ...       "ClientId" : "100", ...       "ClientName" : "John", ...       "ClientPersonalDetails" : [ ...          { ...             "ClientCountryName" : "US", ...             "ClientProjectName" : "Online Book Store", ... ...          }, ...          { ...             "ClientCountryName" : "AUS", ...     ... Read More

How do I change a MongoDB user's password?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

995 Views

You need to use changeUserPassword() to change a user’s password. Let us first create a user with some roles. Following is the query to create a user in MongoDB −> use admin switched to db admin > db.createUser( ...    { ...       user: "Chris", ...       pwd: "chris", ...       roles: [ { role: "readWrite", db: "test" } ] ...    } ... ); Successfully added user: {    "user" : "Chris",    "roles" : [       {          "role" : "readWrite",          "db" : ... Read More

Advertisements