Found 1659 Articles for Big Data Analytics

Retrieving an embedded object as a document via the aggregation framework in MongoDB?

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

237 Views

To retrieve an embedded object as a document, use the aggregation $replaceRoot. Let us first create a collection with documents −> db.embeddedObjectDemo.insertOne(    { _id: new ObjectId(),       "UserDetails": { "UserName": "John", "UserAge": 24, "UserEmailId": "John22@gmail.com" }    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ced580fef71edecf6a1f693") } > db.embeddedObjectDemo.insertOne( { _id: new ObjectId(), "UserDetails": { "UserName": "Carol", "UserAge": 26, "UserEmailId": "Carol123@gmail.com" } } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ced5828ef71edecf6a1f694") }Following is the query to display all documents from a collection with the help of find() method −> db.embeddedObjectDemo.find().pretty();This will produce the ... Read More

Query array of nested string with MongoDB?

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

126 Views

To query array of nested string, you can use the dot(.) notation. Let us first create a collection with documents −> db.nestedStringDemo.insertOne(    {       "CustomerName": "John",       "CustomerOtherDetails": [ { "Age":29, "CountryName": "US" },       { "CompanyName": "Amazon",       "Salary": 150000, "ProjectName": ["Online Library Management System", "Pig Dice Game"]    } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea4629ef71edecf6a1f690") } > db.nestedStringDemo.insertOne( {    "CustomerName": "Chris",    "CustomerOtherDetails": [ { "Age":27, "CountryName": "AUS" },    { "CompanyName": "Google",       "Salary": 250000, "ProjectName": ["Chat Application", "Game ... Read More

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({"maxLengthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh":"This is demo"}); {    "acknowledged" : true,    "insertedId" : ObjectId("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

Advertisements