AmitDiwan has Published 10744 Articles

How to validate documents before insert or update in MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:13:38

609 Views

To validate documents, use the concept of validation. Following is the query −> db.createCollection("demo356", {validator: { ... $and: [ {"FirstName": {$type: "string", $exists: true}} ] ... }}) { "ok" : 1 }Let us create a collection with documents −> db.demo356.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e568d49f8647eb59e5620c7") } ... Read More

How to project specific elements in an array field with MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:10:30

3K+ Views

To project-specific elements in an array field, use $project. Let us create a collection with documents −>db.demo355.insertOne({"id":101, "details":[{"Name":"Chris", isMarried:1}, {"Name":"David", isMarried:0}, {"Name":"Mike", isMarried:1}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e568928f8647eb59e5620c5") }Display all documents from a collection with the help of find() method −> db.demo355.find().pretty();This will produce the following ... Read More

MongoDB query for counting number of unique fields grouped by another field?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:07:37

310 Views

For this, use aggregate() and group with $group. Let us create a collection with documents −> db.demo354.insertOne({"Name1":"Chris", "Name2":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5685a6f8647eb59e5620c0") } > db.demo354.insertOne({"Name1":"Chris", "Name2":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5685a9f8647eb59e5620c1") } > db.demo354.insertOne({"Name1":"Chris", "Name2":"Bob"}); {    "acknowledged" : true,   ... Read More

MongoDB query to find oldest date of three keys in each document

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:04:33

571 Views

To find the oldest date, use $min in MongoDB aggregate(). Let us create a collection with documents −> db.demo353.insertOne({"Date1":new ISODate("2019-01-10"), "Date2":new ISODate("2016-01-21"), "Date3":new ISODate("2020-04-11")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e568261f8647eb59e5620be") } > db.demo353.insertOne({"Date1":new ISODate("2011-11-20"), "Date2":new ISODate("2013-12-10"), "Date3":new ISODate("2014-01-05")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56827ff8647eb59e5620bf") ... Read More

MongoDB query to update selected fields

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:02:15

490 Views

To update selected fields, use UPDATE() in MongoDB. The $set is used to set the new value. Let us create a collection with documents −> db.demo352.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e55510af8647eb59e5620ba") } > db.demo352.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e55510ef8647eb59e5620bb") } > db.demo352.insertOne({"Name":"Bob"}); ... Read More

How to find specific array elements in MongoDB document with query and filter with range?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:00:44

206 Views

For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo351.insertOne( ... { ... ...    "_id" : "101", ...    "ProductDetails" : [ ...       { ...          "ProductName" : "Product-1", ...          "ProductPrice" :500 ...   ... Read More

Query deeply nested Objects in MongoDB

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 07:56:23

2K+ Views

To query deeply nested objects, use dot(.) notation in MongoDB. Let us create a collection with documents −> db.demo350.insertOne( ... { ...    id:101, ...    Name: "Chris", ...    details: [ ...       { ...          _id: 1, ...          ClientNumber: ... Read More

MongoDB findOneAndUpdate() to update a single document

AmitDiwan

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" ... Read More

How to unset objects in MongoDB?

AmitDiwan

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" ...          } ... ... Read More

Check duplicates of certain field for inner array in MongoDB

AmitDiwan

AmitDiwan

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

434 Views

To check duplicates in the inner array, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo347.insertOne( ...    { ...       "details": { ...          "details1": [ ...             { ...           ... Read More

Advertisements