Found 1659 Articles for Big Data Analytics

Retrieve only a single document specifying a criteria in MongoDB?

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

336 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

118 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

376 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

178 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

602 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

MongoDB query to display all the fields value, except _id

AmitDiwan
Updated on 15-May-2020 06:50:45

346 Views

Let us create a collection with documents −> db.demo590.insert([ ...    { "Name": "Chris", "Age": 21 }, ...    {"Name": "Bob", "Age": 20}, ...    { "Name": "Sam", "Age": 19 } ... ]); BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" : [ ],    "nInserted" : 3,    "nUpserted" : 0,    "nMatched" : 0,    "nModified" : 0,    "nRemoved" : 0,    "upserted" : [ ] })Display all documents from a collection with the help of find() method −> db.demo590.find();This will produce the following output −{ "_id" : ObjectId("5e92d514fd2d90c177b5bcd0"), "Name" : "Chris", "Age" : 21 } { ... Read More

MongoDB query to find “where” Billing Address is Equal to Delivery Address from documents?

AmitDiwan
Updated on 15-May-2020 06:48:31

121 Views

To check equality and fetch the documents, use $where in MongoDB. Let us create a collection with documents −> db.demo589.insertOne({deliveryAddress:"US", billingAddress:"UK"});{    "acknowledged" : true, "insertedId" : ObjectId("5e92c117fd2d90c177b5bccc") } > db.demo589.insertOne({deliveryAddress:"US", billingAddress:"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e92c11bfd2d90c177b5bccd") } > db.demo589.insertOne({deliveryAddress:"US", billingAddress:"AUS"});{    "acknowledged" : true, "insertedId" : ObjectId("5e92c11ffd2d90c177b5bcce") } > db.demo589.insertOne({deliveryAddress:"UK", billingAddress:"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e92c127fd2d90c177b5bccf") }Display all documents from a collection with the help of find() method −> db.demo589.find();This will produce the following output −{ "_id" : ObjectId("5e92c117fd2d90c177b5bccc"), "deliveryAddress" : "US", "billingAddress" : "UK" } { "_id" : ObjectId("5e92c11bfd2d90c177b5bccd"), "deliveryAddress" : "US", "billingAddress" : ... Read More

MongoDB query to gather unique array item?

AmitDiwan
Updated on 15-May-2020 06:46:31

194 Views

To gather a unique array items, use distinct(). Let us create a collection with documents −> db.demo588.insertOne({"CountryName":["US","AUS","UK","US","UK","AUS"]});{    "acknowledged" : true, "insertedId" : ObjectId("5e92bbd2fd2d90c177b5bccb") }Display all documents from a collection with the help of find() method −> db.demo588.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e92bbd2fd2d90c177b5bccb"),    "CountryName" : [       "US",       "AUS",       "UK",       "US",       "UK",       "AUS"    ] }Following is the query to gather unique array item −> db.demo588.distinct("CountryName");This will produce the following output −[ "AUS", "UK", "US" ]

How to keep appending subdocuments in MongoDB?

AmitDiwan
Updated on 15-May-2020 06:45:17

98 Views

To append subdocuments, use $push in MongoDB. The update() is used to update. Let us create a collection with documents −> db.demo587.insertOne({"id":101, "details":[{Name:"Chris", Age:21, Marks:57}]});{    "acknowledged" : true, "insertedId" : ObjectId("5e92ba01fd2d90c177b5bcc9") } > db.demo587.insertOne({"id":102, "details":[{Name:"Bob", Age:22, Marks:78}]});{    "acknowledged" : true, "insertedId" : ObjectId("5e92ba0efd2d90c177b5bcca") }Display all documents from a collection with the help of find() method −> db.demo587.find();This will produce the following output −{ "_id" : ObjectId("5e92ba01fd2d90c177b5bcc9"), "id" : 101, "details" : [ { "Name" : "Chris", "Age" : 21, "Marks" : 57 } ] } { "_id" : ObjectId("5e92ba0efd2d90c177b5bcca"), "id" : 102, "details" : [ { "Name" : ... Read More

Advertisements