Query Array of Embedded Documents in MongoDB Based on Range

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

212 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

Query Objects with the Longest Time Period in MongoDB

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

152 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

Convert Array of ObjectIDs to Embedded Documents in JavaScript

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 Specific Fields in Nested Array Documents

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

829 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

465 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

Get Single Document Using findById in MongoDB

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

Zip Two Arrays and Create New Array of Object in a Reshaped Form with MongoDB

AmitDiwan
Updated on 02-Apr-2020 07:19:19

234 Views

For this, use aggregate along with $zip. The zip is used to transpose an array. Let us create a collection with documents −> db.demo339.insertOne({Id:101, Score1:["98", "56"], Score2:[67, 89]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e529ee5f8647eb59e5620a2") }Display all documents from a collection with the help of find() method −> db.demo339.find();This will produce the following output −{ "_id" : ObjectId("5e529ee5f8647eb59e5620a2"), "Id" : 101, "Score1" : [ "98", "56" ], "Score2" : [ 67, 89 ] }Following is the query to zip two arrays with $zip and create a new array of object −> db.demo339.aggregate([ ...    { ...     ... Read More

Working with MongoDB concatArrays in Project on Existing Multi-Array Field

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

383 Views

The $concatArrays is used to concatenate arrays to return the concatenated array.Let us create a collection with documents −> db.demo338.insertOne({"Name":"Chris", "Marks1":[ [56, 67, 45], [67, 89, 90, 91]]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5299baf8647eb59e56209f") }Display all documents from a collection with the help of find() method −> db.demo338.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e5299baf8647eb59e56209f"),    "Name" : "Chris",    "Marks1" : [       [          56,          67,          45       ],       [         ... Read More

Calculate Sum in MongoDB with Aggregate

AmitDiwan
Updated on 02-Apr-2020 06:58:31

2K+ Views

To get the sum, use $sum along with aggregate(). Let us create a collection with documents −> db.demo337.insertOne({"Amount":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5231e5f8647eb59e56209b") } > db.demo337.insertOne({"Amount":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5231e9f8647eb59e56209c") } > db.demo337.insertOne({"Amount":400}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5231ebf8647eb59e56209d") }Display all documents from a collection with the help of find() method −> db.demo337.find();This will produce the following output −{ "_id" : ObjectId("5e5231e5f8647eb59e56209b"), "Amount" : 100 } { "_id" : ObjectId("5e5231e9f8647eb59e56209c"), "Amount" : 500 } { "_id" : ObjectId("5e5231ebf8647eb59e56209d"), "Amount" : 400 }Following is the query to calculate sum ... Read More

Advertisements