Found 1349 Articles for MongoDB

How to delete document by _id using MongoDB?

AmitDiwan
Updated on 13-May-2020 07:09:14

11K+ Views

To delete by _id, use remove() in MongoDB. Following is the syntax −db.yourCollectionName.remove({_id:yourObjectId});To understand the above syntax, let us create a collection with documents −> db.demo518.insertOne({"ClientName":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88b02db3fbf26334ef610e") } > db.demo518.insertOne({"ClientName":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88b030b3fbf26334ef610f") } > db.demo518.insertOne({"ClientName":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88b035b3fbf26334ef6110") }Display all documents from a collection with the help of find() method −> db.demo518.find();This will produce the following output −{ "_id" : ObjectId("5e88b02db3fbf26334ef610e"), "ClientName" : "Chris" } { "_id" : ObjectId("5e88b030b3fbf26334ef610f"), "ClientName" : "Bob" } { "_id" : ObjectId("5e88b035b3fbf26334ef6110"), "ClientName" : "David" }Following is ... Read More

How can I change the field name in MongoDB?

AmitDiwan
Updated on 13-May-2020 07:08:15

174 Views

To change the field name, use the $project. Let us create a collection with documents −> db.demo517.insertOne({"Name":"Chris Brown"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88a2a2987b6e0e9d18f595") } > db.demo517.insertOne({"Name":"David Miller"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88a2ab987b6e0e9d18f596") } > db.demo517.insertOne({"Name":"John Doe"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88a2b1987b6e0e9d18f597") }Display all documents from a collection with the help of find() method −> db.demo517.find();This will produce the following output −{ "_id" : ObjectId("5e88a2a2987b6e0e9d18f595"), "Name" : "Chris Brown" } { "_id" : ObjectId("5e88a2ab987b6e0e9d18f596"), "Name" : "David Miller" } { "_id" : ObjectId("5e88a2b1987b6e0e9d18f597"), "Name" : "John Doe" }Following is the query to ... Read More

Update the last row with search criteria in MongoDB?

AmitDiwan
Updated on 13-May-2020 07:06:42

296 Views

To update with search criteria, use findAndModify() in MongoDB. Let us create a collection with documents −> db.demo516.insertOne({"Name":"John", "Age":22, "Score":56});{    "acknowledged" : true,    "insertedId" : ObjectId("5e889fdb987b6e0e9d18f591") } > db.demo516.insertOne({"Name":"John", "Age":23, "Score":67});{    "acknowledged" : true,    "insertedId" : ObjectId("5e889ff1987b6e0e9d18f592") } > db.demo516.insertOne({"Name":"John", "Age":22, "Score":56});{    "acknowledged" : true,    "insertedId" : ObjectId("5e889ff3987b6e0e9d18f593") } > db.demo516.insertOne({"Name":"John", "Age":22, "Score":66});{    "acknowledged" : true,    "insertedId" : ObjectId("5e889ffa987b6e0e9d18f594") }Display all documents from a collection with the help of find() method −> db.demo516.find();This will produce the following output −{ "_id" : ObjectId("5e889fdb987b6e0e9d18f591"), "Name" : "John", "Age" : 22, "Score" : 56 } ... Read More

Unwind two arrays from MongoDB

AmitDiwan
Updated on 13-May-2020 07:04:38

1K+ Views

To unwind, use $unwind. The $unwind deconstructs an array field from the input documents to output a document for each element.Let us create a collection with documents −> db.demo515.insertOne( ... { ...    "details1": [ ...       "4700100004" ...    ], ...    "details2": [ ...       "Not Given" ...    ], ...    "Value1": [ ...       "56", ...       "45", ...       "35", ...    ], ...    "Value2": [ ...       "35", ...       "45", ...       "56", ...    ]} ... Read More

MongoDB: Find name similar to Regular Expression input?

AmitDiwan
Updated on 13-May-2020 07:02:14

59 Views

Send the name with $regex in MongoDB to find a name similar to the input. Let us create a collection with documents −> db.demo514.insertOne({"Information":{"FullName":"John Doe"}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e885116987b6e0e9d18f58c") } > db.demo514.insertOne({"Information":{"FullName":"John Smith"}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88515e987b6e0e9d18f58d") } > db.demo514.insertOne({"Information":{"FullName":"john doe"}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e885169987b6e0e9d18f58e") } > db.demo514.insertOne({"Information":{"FullName":"Chris Brown"}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88516f987b6e0e9d18f58f") }Display all documents from a collection with the help of find() method −> db.demo514.find();This will produce the following output −{ "_id" : ObjectId("5e885116987b6e0e9d18f58c"), "Information" : { "FullName" : "John Doe" } ... Read More

Sum the score of duplicate column values in MongoDB documents?

AmitDiwan
Updated on 13-May-2020 07:00:34

126 Views

To sum values in different documents, use MongoDB $group. Let us create a collection with documents −> db.demo512.insertOne({"Name":"Chris", "Score1":45, "Score2":65, "CountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e884d96987b6e0e9d18f588") } > db.demo512.insertOne({"Name":"Chris", "Score1":41, "Score2":45, "CountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e884da2987b6e0e9d18f589") } > db.demo512.insertOne({"Name":"Bob", "Score1":75, "Score2":55, "CountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e884db2987b6e0e9d18f58a") } > db.demo512.insertOne({"Name":"Bob", "Score1":65, "Score2":90, "CountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e884dc2987b6e0e9d18f58b") }Display all documents from a collection with the help of find() method −> db.demo512.find();This will produce the following output −{ "_id" : ObjectId("5e884d96987b6e0e9d18f588"), "Name" : "Chris", "Score1" : 45, ... Read More

MongoDB query to find matching documents given an array with values?

AmitDiwan
Updated on 13-May-2020 06:56:14

145 Views

For specific documents, use MongoDB $in. Let us create a collection with documents −> db.demo511.insertOne({"ListOfProject":["Library Management System", "Hospital Management System"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e88473a987b6e0e9d18f585") } > db.demo511.insertOne({"ListOfProject":["Online Web Tracking", "Library Management System"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e884751987b6e0e9d18f586") } > db.demo511.insertOne({"ListOfProject":["Online Shopping Cart", "Hospital Management System"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e884776987b6e0e9d18f587") }Display all documents from a collection with the help of find() method −> db.demo511.find();This will produce the following output −{ "_id" : ObjectId("5e88473a987b6e0e9d18f585"), "ListOfProject" : [ "Library Management System", "Hospital Management System" ] } { "_id" : ... Read More

How to match and group array elements with the max value in MongoDB aggregation?

AmitDiwan
Updated on 13-May-2020 06:54:21

509 Views

For this, use $group along with $max in MongoDB. Let us create a collection with documents −> db.demo510.insertOne( ... { ...    details:[ ...       { ...          Name:"Chris", ...          Score:56 ...       }, ...       { ...          Name:"David", ...          Score:45 ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e8845fa987b6e0e9d18f582") } > db.demo510.insertOne( ... { ...    details:[ ...       { ...       ... Read More

Implement MongoDB $addToSet for an array in an array and append a value

AmitDiwan
Updated on 13-May-2020 06:48:29

297 Views

For this, use update() along with $addToSet. The $addToSet operator adds value to an array unless the value is already present, in which case $addToSet does nothing to that array. Let us create a collection with documents −> db.demo509.insertOne( ... { ... ...    "value1" : [ ...       { ...          "value2" : [ ...             76, ...             14, ...             56 ...          ] ...       }, ...       { ... Read More

Does MongoDB track how many times each index is used in a query?

AmitDiwan
Updated on 13-May-2020 06:38:55

72 Views

Yes, you can track how many times each index is used in a query using MongoDB $indexStats. Following is the query to create an index in MongoDB −> db.demo508.createIndex({"FirstName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Let us create a collection with documents −> db.demo508.insertOne({"FirstName":"John"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e883818987b6e0e9d18f578") } > db.demo508.insertOne({"FirstName":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88381b987b6e0e9d18f579") } > db.demo508.insertOne({"FirstName":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88381f987b6e0e9d18f57a") }Display all documents from a collection with the help of find() method −> db.demo508.find();This will ... Read More

Advertisements