Found 1359 Articles for MongoDB

How to use custom variable while updating a MongoDB document?

AmitDiwan
Updated on 15-May-2020 07:10:44

380 Views

To update, use update() and following is the syntax to create and use a sample custom variable −var anyVariableName=yourValue; db.yourCollectionName.update({filter}, {$set:{yourFieldName:yourVariableName}});Let us create a collection with documents −> db.demo600.insertOne({id:1, Name:"Robert"});{    "acknowledged" : true, "insertedId" : ObjectId("5e94a063f5f1e70e134e2699") } > db.demo600.insertOne({id:2, Name:"Mike"});{    "acknowledged" : true, "insertedId" : ObjectId("5e94a06bf5f1e70e134e269a") } > db.demo600.insertOne({id:3, Name:"Sam"});{    "acknowledged" : true, "insertedId" : ObjectId("5e94a072f5f1e70e134e269b") }Display all documents from a collection with the help of find() method −> db.demo600.find();This will produce the following output −{ "_id" : ObjectId("5e94a063f5f1e70e134e2699"), "id" : 1, "Name" : "Robert" } { "_id" : ObjectId("5e94a06bf5f1e70e134e269a"), "id" : 2, "Name" : "Mike" } ... Read More

How to subtract values (TotalPrice – Discount) from document field values in MongoDB?

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

288 Views

To subtract values from document field values, use $subtract in MongoDB aggregate(). Let us create a collection with documents −> db.demo599.insertOne({"TotalPrice":250, "DiscountPrice":35});{    "acknowledged" : true, "insertedId" : ObjectId("5e948192f5f1e70e134e2696") } > db.demo599.insertOne({"TotalPrice":400, "DiscountPrice":10});{    "acknowledged" : true, "insertedId" : ObjectId("5e948199f5f1e70e134e2697") } > db.demo599.insertOne({"TotalPrice":1550, "DiscountPrice":50});{    "acknowledged" : true, "insertedId" : ObjectId("5e9481a0f5f1e70e134e2698") }Display all documents from a collection with the help of find() method −> db.demo599.find();This will produce the following output −{ "_id" : ObjectId("5e948192f5f1e70e134e2696"), "TotalPrice" : 250, "DiscountPrice" : 35 } { "_id" : ObjectId("5e948199f5f1e70e134e2697"), "TotalPrice" : 400, "DiscountPrice" : 10 } { "_id" : ObjectId("5e9481a0f5f1e70e134e2698"), "TotalPrice" : 1550, "DiscountPrice" ... Read More

MongoDB Aggregate to get average from document and of array elements?

AmitDiwan
Updated on 15-May-2020 07:06:03

314 Views

For this, use $avg along with $group and aggregate(). Let us create a collection with documents −> db.demo598.insertOne( ...    { ...       Information:'Student', ...       id:100, ...       details:[ ...          {Name:'Chris', Marks:75}, ...          {Name:'Bob', Marks:55} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e947fccf5f1e70e134e2694") } > db.demo598.insertOne( ...    { ...       Information:'Student', ...       id:101, ...       details:[ ...          {Name:'Chris', Marks:75}, ...     ... Read More

Find which MongoDB document contains a specific string?

AmitDiwan
Updated on 15-May-2020 07:03:27

682 Views

To find which document contains a specific string, use $regex along with find(). Let us create a collection with documents −> db.demo597.insertOne({"Name":"John Doe"});{    "acknowledged" : true, "insertedId" : ObjectId("5e947ae3f5f1e70e134e2690") } > db.demo597.insertOne({"Name":"John Smith"});{    "acknowledged" : true, "insertedId" : ObjectId("5e947ae8f5f1e70e134e2691") } > db.demo597.insertOne({"Name":"Chris Brown"});{    "acknowledged" : true, "insertedId" : ObjectId("5e947aeff5f1e70e134e2692") } > db.demo597.insertOne({"Name":"Adam Smith"});{    "acknowledged" : true, "insertedId" : ObjectId("5e947afff5f1e70e134e2693") }Display all documents from a collection with the help of find() method −> db.demo597.find();This will produce the following output −{ "_id" : ObjectId("5e947ae3f5f1e70e134e2690"), "Name" : "John Doe" } { "_id" : ObjectId("5e947ae8f5f1e70e134e2691"), "Name" : "John Smith" } ... Read More

Retrieve only a single document specifying a criteria in MongoDB?

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

324 Views

Use findOne() in MongoDB for this. The findOne() returns one document that satisfies the specified query criteria on the collection.Let us create a collection with documents −> db.demo596.insertOne({_id:1, "FirstName":"John", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo596.insertOne({_id:2, "FirstName":"John", "LastName":"Doe"}); { "acknowledged" : true, "insertedId" : 2 } > db.demo596.insertOne({_id:3, "FirstName":"Chris", "LastName":"Brown"}); { "acknowledged" : true, "insertedId" : 3 } > db.demo596.insertOne({_id:4, "FirstName":"David", "LastName":"Miller"}); { "acknowledged" : true, "insertedId" : 4 }Display all documents from a collection with the help of find() method −> db.demo596.find();This will produce the following output −{ "_id" : 1, "FirstName" : "John", "LastName" ... Read More

MongoDB query to update nested document

AmitDiwan
Updated on 15-May-2020 07:01:00

2K+ Views

Let us create a collection with documents −> db.demo595.insertOne( { "Information": [    { "_id": new ObjectId(), Name:"Chris" },    { _id:new ObjectId(), Name:"Robert" } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e93369cfd2d90c177b5bce4") }Display all documents from a collection with the help of find() method −> db.demo595.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e93369cfd2d90c177b5bce4"),    "Information" : [       {          "_id" : ObjectId("5e93369cfd2d90c177b5bce2"),          "Name" : "Chris"       },       {          "_id" : ObjectId("5e93369cfd2d90c177b5bce3"),     ... Read More

MongoDB query to limit the returning values of a field?

AmitDiwan
Updated on 15-May-2020 06:58:21

103 Views

For this, use $slice. Let us create a collection with documents −> db.demo594.insertOne( ...    { ...       id:1, ...       details:[ ...          {Name:"Chris", Age:21}, ...          {Name:"Bob", Age:20}, ...          {Name:"David", Age:23}, ...          {Name:"Sam", Age:22} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e933459fd2d90c177b5bcdd") }Display all documents from a collection with the help of find() method −> db.demo594.find();This will produce the following output −{ "_id" : ObjectId("5e933459fd2d90c177b5bcdd"), "id" : 1, "details" ... Read More

Fetch specific multiple documents in MongoDB

AmitDiwan
Updated on 15-May-2020 06:56:33

359 Views

To fetch specific multiple documents in MongoDB, use $in. Let us create a collection with documents −> db.demo593.insertOne({id:1, "Name":"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e93177dfd2d90c177b5bcd9") } > db.demo593.insertOne({id:2, "Name":"John"});{    "acknowledged" : true, "insertedId" : ObjectId("5e931785fd2d90c177b5bcda") } > db.demo593.insertOne({id:3, "Name":"Bob"});{    "acknowledged" : true, "insertedId" : ObjectId("5e93178cfd2d90c177b5bcdb") } > db.demo593.insertOne({id:4, "Name":"Sam"});{    "acknowledged" : true, "insertedId" : ObjectId("5e931792fd2d90c177b5bcdc") }Display all documents from a collection with the help of find() method −> db.demo593.find();This will produce the following output −{ "_id" : ObjectId("5e93177dfd2d90c177b5bcd9"), "id" : 1, "Name" : "Chris" } { "_id" : ObjectId("5e931785fd2d90c177b5bcda"), "id" : 2, "Name" : "John" } ... Read More

MongoDB query to match documents that contain an array field

AmitDiwan
Updated on 15-May-2020 06:54:47

164 Views

To match documents that contain an array field, use the $elemMatch operator. Let us create a collection with documents −> db.demo592.insertOne( ...    { ...       "id":101, ...       "details" : [ ...          { "Name" : "Chris", "Value" : "200"}, ...          {"Name" : "David", "Value" : "800"} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e930d8ffd2d90c177b5bcd6") } > db.demo592.insertOne( ... { ...    id:102, ...    "details" : [ ...       { "Name" : "Chris", "Value" ... Read More

MongoDB find() query for nested document?

AmitDiwan
Updated on 15-May-2020 06:52:22

570 Views

To fetch a value from the nested document, use dot notation. Let us create a collection with documents −> db.demo591.insert([ ...    { "Name": "John", "Age": 23 }, ...    {"Name": "Carol", "Age": 26}, ...    { "Name": "Robert", "Age": 29, ...    details:[ ...       { ...          Email:"Robert@gmail.com", CountryName:"US"}, {"Post":35} ...       ]} ... ]); BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" : [ ],    "nInserted" : 3,    "nUpserted" : 0,    "nMatched" : 0,    "nModified" : 0,    "nRemoved" : 0,    "upserted" : [ ] ... Read More

Previous 1 ... 6 7 8 9 10 ... 136 Next
Advertisements