Found 6705 Articles for Database

MongoDB findOneAndUpdate() to update a single document

AmitDiwan
Updated on 02-Apr-2020 07:53:36

318 Views

The findOneAndUpdate() is used to update only a single document in MongoDB. Let us create a collection with documents −db.demo349.insertOne({"Name":"Chris", "Marks":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e55384af8647eb59e5620b4") } > db.demo349.insertOne({"Name":"David", "Marks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e553853f8647eb59e5620b5") } > db.demo349.insertOne({"Name":"Chris", "Marks":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e55385af8647eb59e5620b6") } > db.demo349.insertOne({"Name":"David", "Marks":54}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e55385ff8647eb59e5620b7") }Display all documents from a collection with the help of find() method −> db.demo349.find();This will produce the following output −{ "_id" : ObjectId("5e55384af8647eb59e5620b4"), "Name" : "Chris", "Marks" : 56 } { ... Read More

How to unset objects in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 07:51:40

284 Views

To unset objects in MongoDB, use $unset. Let us create a collection with documents −> db.demo348.insertOne( ...    { ...    "details": { ...       "studentDetails": ...          { ...             StudentName: "Robert" ...          } ...    } ... ... }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e553556f8647eb59e5620b2") } > db.demo348.insertOne( ... { ...    "details": { ...       "studentDetails": ...          { ...             StudentName: "Mike" ...          } ... ... Read More

Check duplicates of certain field for inner array in MongoDB

AmitDiwan
Updated on 02-Apr-2020 07:48:40

433 Views

To check duplicates in the inner array, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo347.insertOne( ...    { ...       "details": { ...          "details1": [ ...             { ...                Name: "Chris", ...                Age: 21 ...             } ... ...          ] ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5532eaf8647eb59e5620af") } > ... Read More

Querying array of Embedded Documents in MongoDB based on Range?

AmitDiwan
Updated on 02-Apr-2020 07:44:54

211 Views

To query an array of embedded documents based on range, use aggregate(). Let us create a collection with documents −> db.demo346.insertOne( ...    { ...       _id: 101, ...       userDetails: [ ...          { UserName: "Chris", Score:78}, ...          { UserName: "David", Score:68}, ...          { UserName: "Bob", Score:88} ...       ] ...    } ... ); { "acknowledged" : true, "insertedId" : 101 } > db.demo346.insertOne( ...    { ...       _id: 102, ...       userDetails: [ ...   ... Read More

MongoDB query to find and return subdocument with criteria?

AmitDiwan
Updated on 02-Apr-2020 07:40:05

551 Views

Let us create a collection with documents −> db.demo345.insertOne({ ...    "UserName" : "Robert", ...       "UserDetails" : [ ...          { ...             "isMarried" : false, ...             "CountryName":"US" ... ...          }, ...          { ...             "isMarried" : true, ...             "CountryName":"UK" ... ...          }, ...          { ...             "isMarried" : false, ... ... Read More

How to query objects with the longest time period in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 07:36:09

151 Views

Let us create a collection with documents −> db.demo344.insertOne({"startDate":"2020-02-24 10:50:00", "endDate":"2020-02-24 11:50:00"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e53f52cf8647eb59e5620aa") } > db.demo344.insertOne({"startDate":"2020-02-24 08:00:00", "endDate":"2020-02-24 11:50:50"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e53f53df8647eb59e5620ab") }Display all documents from a collection with the help of find() method −> db.demo344.find();This will produce the following output −{ "_id" : ObjectId("5e53f52cf8647eb59e5620aa"), "startDate" : "2020-02-24 10:50:00", "endDate" : "2020-02-24 11:50:00" } { "_id" : ObjectId("5e53f53df8647eb59e5620ab"), "startDate" : "2020-02-24 08:00:00", "endDate" : "2020-02-24 11:50:50" }Following is how to query objects with the longest time period −> db.demo344.aggregate([ ...    { $addFields: { ...   ... Read More

How do you convert an array of ObjectIds into an array of embedded documents with a field containing the original array element value?

AmitDiwan
Updated on 02-Apr-2020 07:34:12

90 Views

For such conversion, use aggregate. Let us create a collection with documents −> db.demo343.insertOne({ ...    _id: 101, ...    UserName: "Chris", ...    details: [ ...       {"Name":"John"}, ...       {"Name":"David"} ...    ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo343.find().pretty();This will produce the following output −{    "_id" : 101,    "UserName" : "Chris",    "details" : [       {          "Name" : "John"       },       ... Read More

MongoDB query to get only specific fields in nested array documents?

AmitDiwan
Updated on 02-Apr-2020 07:30:22

827 Views

To get only specific fields in nested array documents, use $filter along with $project. Let us create a collection with documents −> db.demo342.insertOne({ ...    "Id": "101", ...       "details1" : { ...          "details2" : [ ...             { ...                "details3" : [ ...                   { ...                      "Name": "Mike", ...                      "CountryName" : "US" ... ... Read More

MongoDB query to find value in array with multiple criteria (range)

AmitDiwan
Updated on 02-Apr-2020 07:26:04

464 Views

To find value in an array within a range, use $gt and $lt. Let us create a collection with documents −> db.demo341.insertOne({ ...    "Name": "Chris", ...       "productDetails" : [ ...          { ...             "ProductPrice" : { ...             "Price" : 800 ...          } ...       }, ...       { ...          "ProductPrice" : { ...             "Price" : 400 ...          } ... ... Read More

MongoDB findById returning a list of documents instead of a single result? How to get only a single document?

AmitDiwan
Updated on 02-Apr-2020 07:20:53

236 Views

To get only a single result, use findOne() and fetch on the basis of id. Let us create a collection with documents −> db.demo340.insertOne({_id:1, "Name":"Chris", Age:21}); { "acknowledged" : true, "insertedId" : 1 } > db.demo340.insertOne({_id:2, "Name":"David", Age:23}); { "acknowledged" : true, "insertedId" : 2 } > db.demo340.insertOne({_id:3, "Name":"Bob", Age:20}); { "acknowledged" : true, "insertedId" : 3 } > db.demo340.insertOne({_id:4, "Name":"Sam", Age:19}); { "acknowledged" : true, "insertedId" : 4 }Display all documents from a collection with the help of find() method −> db.demo340.find();This will produce the following output −{ "_id" : 1, "Name" : "Chris", "Age" : 21 } { ... Read More

Advertisements