Found 1659 Articles for Big Data Analytics

MongoDB query to update tag

AmitDiwan
Updated on 11-May-2020 09:27:11

196 Views

To update tag in MongoDB, use the update command. Let us create a collection with documents −> db.demo713.insertOne( ... { ... tags: ...    [ ...       { ...          id:101, ...          Name:"Tag-1" ...       }, ...       { ...          id:102, ...          Name:"Tag-3" ...       }, ...       { ...          id:103, ...          Name:"Tag-3" ...       } ...    ] ... } ... ); {   ... Read More

Cast to ObjectId failed for value in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:24:54

564 Views

To cast to ObjectId correctly, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo460.insertOne({"_id":"5ab9cbfa31c2ab715d42129e"}); { "acknowledged" : true, "insertedId" : "5ab9cbfa31c2ab715d42129e" }Display all documents from a collection with the help of find() method −> db.demo460.find();This will produce the following output −{ "_id" : "5ab9cbfa31c2ab715d42129e" }Following is the query to cast to objectId −> db.demo460.aggregate( [ idConvert ]) This will produce the following output −{ "_id" : "5ab9cbfa31c2ab715d42129e", "value" : ObjectId("5ab9cbfa31c2ab715d42129e") }

How to get items from an object array in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:22:47

810 Views

To get items from an object array, use aggregate(). Let us create a collection with documents −> db.demo459.insertOne( ... { "_id" : 1, ... "Information" : [ ...    { ...       "Name" : "Chris", ...       "_id" : new ObjectId(), ...       "details" : [ ...          "HR" ...       ] ...    }, ... { ... ...    "Name" : "David", ...    "_id" : new ObjectId(), ...    "details" : [ ...       "Developer" ...    ] ... }, ... { ... ...   ... Read More

How can we update a record in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:22:18

321 Views

To update a record, you need to update on the basis of _id. Let us create a collection with documents −> db.demo458.insertOne( {_id:101, "Name":"David" } ); { "acknowledged" : true, "insertedId" : 101 } > db.demo458.insertOne( {_id:102, "Name":"Chris" } ); { "acknowledged" : true, "insertedId" : 102 } > db.demo458.insertOne( {_id:103, "Name":"Bob" } ); { "acknowledged" : true, "insertedId" : 103 }Display all documents from a collection with the help of find() method −> db.demo458.find();This will produce the following output −{ "_id" : 101, "Name" : "David" } { "_id" : 102, "Name" : "Chris" } { "_id" : 103, ... Read More

How do I return a document with filtered sub-documents using Mongo?

AmitDiwan
Updated on 11-May-2020 09:20:20

105 Views

For this, use $project in MongoDB. Within that, use $filter. Let us create a collection with documents −> db.demo457.insertOne( ... { ...    _id: 101, ...    details: [ ...       { ProductName:"Product-1" , ProductPrice:90 }, ...       { ProductName:"Product-2" , ProductPrice:190 } ...    ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 } > > db.demo457.insertOne( ... { ...    _id: 102, ...    details: [ ...       { ProductName:"Product-3" , ProductPrice:150}, ...       { ProductName:"Product-4" , ProductPrice:360 } ...    ] ... } ... ); { ... Read More

How to aggregate two lists if at least one element matches in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:17:18

219 Views

For this, use $group in MongoDB. Within that, use $unwind, $group, $addToSet, etc. Let us create a collection with documents −> db.demo456.insertOne( ... { _id: 101, StudentName: ["Chris", "David"] } ... ); { "acknowledged" : true, "insertedId" : 101 } > > db.demo456.insertOne( ... { _id: 102, StudentName: ["Mike", "Sam"] } ... ); { "acknowledged" : true, "insertedId" : 102 } > db.demo456.insertOne( ... { _id: 103, StudentName: ["John", "Jace"] } ... ); { "acknowledged" : true, "insertedId" : 103 } > db.demo456.insertOne( ... { _id: 104, StudentName: ["Robert", "John"] } ... ); { "acknowledged" : true, "insertedId" : 104 ... Read More

How do I display a list of objects based on a specific property with MongoDB?

AmitDiwan
Updated on 11-May-2020 09:16:38

256 Views

To display a list of objects based on a specific property, use dot notation in find(). Let us create a collection with documents −> db.demo455.insertOne({"Information":{"Student":[{"Name":"Chris", "Age":22}]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7e1876dbcb9adb296c95c5") } > db.demo455.insertOne({"Information":{"Student":[{"Name":"David", "Age":21}]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7e1883dbcb9adb296c95c6") } > db.demo455.insertOne({"Information":{"Student":[{"Name":"Bob", "Age":24}]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7e188adbcb9adb296c95c7") } > db.demo455.insertOne({"Information":{"Student":[{"Name":"Robert", "Age":21}]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7e18bcdbcb9adb296c95c8") }Display all documents from a collection with the help of find() method −> db.demo455.find();This will produce the following output −{ "_id" : ObjectId("5e7e1876dbcb9adb296c95c5"), "Information" : { "Student" : [ { ... Read More

How to continuously publish the latest N records with sorting using MongoDB?

AmitDiwan
Updated on 11-May-2020 09:14:18

66 Views

To publish the latest N records with sorting, use sort() along with limit(). Here, set the number of records you want to show with limit(). Let us create a collection with documents −> db.demo454.insertOne({"ClientName":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7cce8cdbcb9adb296c95c0") } > db.demo454.insertOne({"ClientName":"John"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7cce95dbcb9adb296c95c1") } > db.demo454.insertOne({"ClientName":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7cce9fdbcb9adb296c95c2") } > db.demo454.insertOne({"ClientName":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7ccea6dbcb9adb296c95c3") } > db.demo454.insertOne({"ClientName":"Mike"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7cceafdbcb9adb296c95c4") }Display all documents from a collection with the help of find() method −> db.demo454.find();This ... Read More

How to filter documents based on an array in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:13:33

928 Views

To filter documents based on an array, use $elemMatch. The $elemMatch operator matches documents that contain an array field.Let us create a collection with documents −> db.demo453.insertOne( ... { _id: 101, details: [ { Name: "David", Marks: 60 }, { Name: "Mike", Marks: 55} ] } ... ) { "acknowledged" : true, "insertedId" : 101 } > db.demo453.insertOne( ... { _id: 102, details: [ { Name: "Bob", Marks: 80 }, { Name: "Sam", Marks: 78} ] } ... ) { "acknowledged" : true, "insertedId" : 102 } > db.demo453.insertOne( ... { _id: 103, details: [ { Name: "Carol", Marks: 67 ... Read More

Get the aggregated result and find the count of repeated values in different MongoDBdocuments

AmitDiwan
Updated on 11-May-2020 09:11:28

114 Views

To get the count of repeated values in different documents, use aggregate(). Let us create a collection with documents −> db.demo452.insertOne({"StudentName":"John", "StudentAge":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e3371f552a0ebb0a6f3") } > db.demo452.insertOne({"StudentName":"John", "StudentAge":22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e3671f552a0ebb0a6f4") } > db.demo452.insertOne({"StudentName":"John", "StudentAge":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e3971f552a0ebb0a6f5") } > db.demo452.insertOne({"StudentName":"David", "StudentAge":24});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e4371f552a0ebb0a6f6") } > db.demo452.insertOne({"StudentName":"David", "StudentAge":25});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e4571f552a0ebb0a6f7") }Display all documents from a collection with the help of find() method −> db.demo452.find();This will produce the following output −{ "_id" : ... Read More

Advertisements