Found 1349 Articles for MongoDB

Replace an array field value with MongoDB?

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

226 Views

You can use positional operator $. Let us first create a collection with documents −> db.replaceAnArrayFieldValueDemo.insertOne({"StudentTechnicalSubjects":["MySQL", "SQL Server", "PL/SQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea41e0ef71edecf6a1f68f") }Following is the query to display all documents from a collection with the help of find() method −> db.replaceAnArrayFieldValueDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cea41e0ef71edecf6a1f68f"),    "StudentTechnicalSubjects" : [       "MySQL",       "SQL Server",       "PL/SQL"    ] }Following is the query to replace an array field value. Here, we are updating “SQL Server” with “MongoDB” −> db.replaceAnArrayFieldValueDemo.update(    {"StudentTechnicalSubjects":"SQL Server"},   ... Read More

Is it possible to use MongoDB field value as pattern in $regex?

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

147 Views

Yes, for this, use $indexOfCP operator along with aggregate framework. Let us first create a collection with documents −> db.patterDemo.insertOne(    {       "ClientName": "John", "ClientWebsiteName":"webbuziness.com/John/business"    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea40acef71edecf6a1f68d") } > db.patterDemo.insertOne(    {       "ClientName": "Carol", "ClientWebsiteName":"solvecoding.com/business"    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea40acef71edecf6a1f68e") }Following is the query to display all documents from a collection with the help of find() method −> db.patterDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cea40acef71edecf6a1f68d"),    "ClientName" : "John",    "ClientWebsiteName" : "abcd.com" ... Read More

Match multiple criteria inside an array with MongoDB?

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

209 Views

For this, use aggregate framework with the $elemMatch operator. Let us first create a collection with documents −> db.matchMultipleCriteriaDemo.insertOne({    "EmployeeDetails": [       {"EmployeeName": "Chris", "Salary": 45000, "Language":"Java"},       {"EmployeeName": "Robert", "Salary": 41000, "Language":"Python"}    ] }); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea3bf0ef71edecf6a1f689") } > db.matchMultipleCriteriaDemo.insertOne({    "EmployeeDetails": [       {"EmployeeName": "David", "Salary": 55000, "Language":"C++"},       {"EmployeeName": "Bob", "Salary": 61000, "Language":"C"}    ] }); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea3bf1ef71edecf6a1f68a") }Following is the query to display all documents from a collection with the help of find() ... Read More

Filter by several array elements in MongoDB?

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

136 Views

For this, you can use $elemMatch operator. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria. Let us first create a collection with documents −> db.filterBySeveralElementsDemo.insertOne(    "_id":100,    "StudentDetails": [       {          "StudentName": "John",          "StudentCountryName": "US",       },       {          "StudentName": "Carol",          "StudentCountryName": "UK"       }    ] } ); { "acknowledged" : true, "insertedId" : 100 } > db.filterBySeveralElementsDemo.insertOne(    { ... Read More

Get the average of marks in MongoDB with aggregate?

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

321 Views

Use $avg operator along with aggregate framework. Let us first create a collection with documents. Here, one of the fields is StudentScore −> db.averageReturiningNullDemo.insertOne(    {"StudentDetails" : { "StudentScore" : 89 } }); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce9822e78f00858fb12e927") } > db.averageReturiningNullDemo.insertOne(    {"StudentDetails" : { "StudentScore" : 34 } }); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce9822e78f00858fb12e928") } > db.averageReturiningNullDemo.insertOne(    {"StudentDetails" : { "StudentScore" : 78 } }); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce9822e78f00858fb12e929") }Following is the query to display all documents from a collection with the help of find() ... Read More

MongoDB. max length of field name?

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

710 Views

MongoDB supports the BSON format data, so there is no max length of field name. Let us first create a collection with documents −>db.maxLengthDemo.insertOne({"maxLengthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh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ectId("5ce97ac978f00858fb12e926") }Following is the query to display all documents from a collection with the help of find() method −> db.maxLengthDemo.find();This will produce the following output.{ "_id" : ObjectId("5ce97ac978f00858fb12e926"), "maxLengthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" : "This is demo" }

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

87 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

691 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

Advertisements