Found 6702 Articles for Database

Update a single list item of a MongoDB document?

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

135 Views

To update a single list item, use positional operator($). Let us first create a collection with documents −> db.updateASingleListDemo.insertOne({ _id:1, "EmployeeName":"Chris", "EmployeeDetails": [ {"EmployeeId":"EMP-101", "EmployeeSalary": 18999 }] }); { "acknowledged" : true, "insertedId" : 1 }Following is the query to display all documents from a collection with the help of find() method −> db.updateASingleListDemo.find().pretty();This will produce the following output −{    "_id" : 1,    "EmployeeName" : "Chris",    "EmployeeDetails" : [       {          "EmployeeId" : "EMP-101",          "EmployeeSalary" : 18999       }    ] }Following is the query ... Read More

Retrieving array values from a find query in MongoDB?

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

89 Views

To retrieve array values, use dot(.) notation. Let us first create a collection with documents −> db.retrievingArrayDemo.insertOne(    { "UserDetails" : [       { "UserName" : "John",  "UserAge" : 23 } ],       "UserCountryName" : "AUS",       "UserLoginDate" : new ISODate(),       "UserMessage" : "Hello"    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce9718478f00858fb12e920") } > db.retrievingArrayDemo.insertOne(    { "UserDetails" : [       { "UserName" : "Sam",  "UserAge" : 24 } ],       "UserCountryName" : "UK",       "UserLoginDate" : new ISODate(),       "UserMessage" : "Bye"    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce9718478f00858fb12e921") }Following is the query to display all documents from a collection with the help of find() method −> db.retrievingArrayDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ce9718478f00858fb12e920"), ... Read More

How can I update child objects in MongoDB?

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

699 Views

To update child objects, use $set operator. Let us first create a collection with document −>db.updateChildObjectsDemo.insertOne({"StudentName":"Chris", "StudentOtherDetails":{"StudentSubject":"MongoDB", "StudentCountryName":"AUS"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce964e078f00858fb12e91f") }Following is the query to display all documents from a collection with the help of find() method −> db.updateChildObjectsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ce964e078f00858fb12e91f"),    "StudentName" : "Chris",    "StudentOtherDetails" : {       "StudentSubject" : "MongoDB",       "StudentCountryName" : "AUS"    } }Following is the query to update child objects in MongoDB −> db.updateChildObjectsDemo.update({"StudentName" : "Chris"}, {$set:{"StudentOtherDetails.StudentCountryName":"UK"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, ... Read More

Convert a field to an array using MongoDB update operation?

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

1K+ Views

To convert a field to an array, use $set operator. Let us first create a collection with documents −> db.convertAFieldToAnArrayDemo.insertOne({"StudentSubject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce92d7778f00858fb12e91d") }Following is the query to display all documents from a collection with the help of find() method −> db.convertAFieldToAnArrayDemo.find();This will produce the following output −{ "_id" : ObjectId("5ce92d7778f00858fb12e91d"), "StudentSubject" : "MongoDB" }Following is the query to convert a field to an array using update operation with $set:−> db.convertAFieldToAnArrayDemo.find().forEach(function(myDocument) {    db.convertAFieldToAnArrayDemo.update(       { _id: myDocument._id },       { "$set": { "StudentSubject": [myDocument.StudentSubject] } }    ); })Let ... Read More

Removing empty fields from MongoDB

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

378 Views

To remove empty fields, use deleteMany(). Let us first create a collection with documents −> db.removeEmptyFieldsDemo.insertOne({"StudentName":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce92b9578f00858fb12e919") } > db.removeEmptyFieldsDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce92b9878f00858fb12e91a") } > db.removeEmptyFieldsDemo.insertOne({"StudentName":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce92b9c78f00858fb12e91b") } > db.removeEmptyFieldsDemo.insertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce92ba078f00858fb12e91c") }Following is the query to display all documents from a collection with the help of find() method −> db.removeEmptyFieldsDemo.find();This will produce the following output −{ "_id" : ObjectId("5ce92b9578f00858fb12e919"), "StudentName" : "" } { "_id" : ObjectId("5ce92b9878f00858fb12e91a"), "StudentName" : "Chris" ... Read More

Creating an index on a nested MongoDB field?

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

1K+ Views

You can use dot(.) notation for this. Let us first create a collection with documents −> db.createIndexOnNestedFieldDemo.insertOne(    {"UserDetails":{"UserPersonalDetails":{"UserFirstName":"John", "UserLastName":"Smith"}}});    {       "acknowledged" : true,       "insertedId" : ObjectId("5ce929c778f00858fb12e916")    } > > db.createIndexOnNestedFieldDemo.insertOne( {"UserDetails":{"UserPersonalDetails":{"UserFirstName":"Chris", "UserLastName":"Brown"}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce929d678f00858fb12e917") } > db.createIndexOnNestedFieldDemo.insertOne( {"UserDetails":{"UserPersonalDetails":{"UserFirstName":"David", "UserLastName":"Miller"}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce929e378f00858fb12e918") }Following is the query to display all documents from a collection with the help of find() method −> db.createIndexOnNestedFieldDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ce929c778f00858fb12e916"),    "UserDetails" : {       ... Read More

Find documents where all elements of an array have a specific value in MongoDB?

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

93 Views

You can use find() for this. Let us first create a collection with documents −> db.findDocumentsDemo.insertOne(    {       _id: 101,       "ProductDetails": [          { "ProductValue":100 },          { "ProductValue":120 }       ]    } ); { "acknowledged" : true, "insertedId" : 101 } > db.findDocumentsDemo.insertOne(    {       _id: 102,       "ProductDetails": [          { "ProductValue":120},          { "ProductValue":120 },          { "ProductValue":120 }       ]    } ); { "acknowledged" ... Read More

Perform MongoDB array concatenation to concatenate records

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

89 Views

For array concatenation, use $concatArrays operator. Let us first create a collection with documents −>db.arrayConcatenationDemo.insertOne({"TeacherName":["Chris", "Robert"], "StudentName":["Mike", "Sam"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce921c078f00858fb12e911") }Following is the query to display all documents from a collection with the help of find() method −> db.arrayConcatenationDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ce921c078f00858fb12e911"),    "TeacherName" : [       "Chris",       "Robert"    ],    "StudentName" : [       "Mike",       "Sam"    ] }Following is the query for array concatenation −> db.arrayConcatenationDemo.aggregate([    { "$project": {       ... Read More

MongoDB query select and display only a specific field from the document?

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

2K+ Views

Let us first create a collection with documents −> db.querySelectDemo.insertOne({UserId:100, UserName:"Chris", UserAge:25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce90eb478f00858fb12e90e") } > db.querySelectDemo.insertOne({UserId:101, UserName:"Robert", UserAge:26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce90ec578f00858fb12e90f") } > db.querySelectDemo.insertOne({UserId:103, UserName:"David", UserAge:27}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce90ed478f00858fb12e910") }Following is the query to display all documents from a collection with the help of find() method −> db.querySelectDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ce90eb478f00858fb12e90e"),    "UserId" : 100,    "UserName" : "Chris",    "UserAge" : 25 } {    "_id" : ObjectId("5ce90ec578f00858fb12e90f"),    "UserId" : 101, ... Read More

MongoDB query to update an array element matching a condition using $push?

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

358 Views

Let us first create a collection with documents −> db.updateArrayElementDemo.insertOne(    {       "UserDetails":       [          {             "UserName":"Chris",             "UserAge":23          }       ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce9029378f00858fb12e90d") }Following is the query to display all documents from a collection with the help of find() method −> db.updateArrayElementDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ce9029378f00858fb12e90d"),    "UserDetails" : [       {       ... Read More

Advertisements