AmitDiwan has Published 10744 Articles

Is it possible to utilize $addToSet multiple times in the same update?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:54:07

339 Views

Yes, it is possible using the UPDATE() method. Let us create a collection with documents −> db.demo27.insertOne({"StudentDetails":{"101":{"Subject":["Java"]}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e15f9e822d07d3b95082e7f") } > db.demo27.insertOne({"StudentDetails":{"101":{"Subject":["MySQL"]}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e15f9eb22d07d3b95082e80") }Display all documents from a collection with the help of find() method ... Read More

Update a single list item of a Mongo DB document and increment it by 1

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:52:40

94 Views

For this, use positional operator($). To increment a field value by 1, use $inc operator. Let us create a collection with documents −>db.demo39.insertOne({"ProductDetails":[{"ProductName":"Product-1", "ProductPrice":349}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176d54cfb11e5c34d898df") } >db.demo39.insertOne({"ProductDetails":[{"ProductName":"Product-2", "ProductPrice":998}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176d61cfb11e5c34d898e0") } >db.demo39.insertOne({"ProductDetails":[{"ProductName":"Product-3", "ProductPrice":145}]}); {   ... Read More

Retrieving array values from a find query in MongoDB database

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:51:13

101 Views

To retrieve array values from a find query, use dot notation. Let us create a collection with documents −> db.demo38.insertOne({"ClientDetails":[{"ClientId":101, "ClientName":"Chris"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176abccfb11e5c34d898d9") } > db.demo38.insertOne({"ClientDetails":[{"ClientId":102, "ClientName":"David"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176ac7cfb11e5c34d898da") } > db.demo38.insertOne({"ClientDetails":[{"ClientId":103, "ClientName":"Mike"}]}); {    "acknowledged" ... Read More

MongoDB query to convert string to int?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:50:17

2K+ Views

To convert string to int, use parseInt() in MongoDB. Let us first create a collection with documents −> db.demo369.insertOne({"Price":"1000000"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57e2e32ae06a1609a00aed") } > db.demo369.insertOne({"Price":"1747864"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57e2e92ae06a1609a00aee") } > db.demo369.insertOne({"Price":"19548575"}); {    "acknowledged" : true,    "insertedId" ... Read More

How to select a specific subdocument in MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:48:25

1K+ Views

To select a specific subdocument in MongoDB, use find(). Let us create a collection with documents −> db.demo37.insertOne({"Details":[{"Name":"Chris", "Age":21}, {"Name":"David", "Age":23}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176635cfb11e5c34d898d7") } > db.demo37.insertOne({"Details":[{"Name":"Sam", "Age":23}, {"Name":"Robert", "Age":25}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e17664acfb11e5c34d898d8") }Display all documents from a ... Read More

MongoDB query to retrieve records from a collection named with letters and numbers

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:48:18

217 Views

At first, let us create a collection with letters and numbers, for example −7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368Access the above collection using db.getCollection(). Let us now create a collection with the name mentioned above −> db.createCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368'); { "ok" : 1 } >db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57e2152ae06a1609a00aea") } >db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').insertOne({"FirstName":"David"}); { ... Read More

MongoDB query to find on the basis of true or false values

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:46:55

1K+ Views

To find on the basis of true or false values, use $exists in find(). You would also need dot notation for the same task.Let us first create a collection with documents −> db.demo367.insertOne( ...    { "Id" : "102", ...    "details" : [ { "Name" : "David"}, ...   ... Read More

Get the length of distinct values in an array with MongoDB

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:46:48

343 Views

To get distinct values, use MongoDB DISTINCT. For length, use LENGTH(). Let us create a collection with documents −> db.demo36.insertOne({"Names":["Chris", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e17643bcfb11e5c34d898d4") } > db.demo36.insertOne({"Names":["Mike", "Sam", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176449cfb11e5c34d898d5") } > db.demo36.insertOne({"Names":["Chris", "Bob", "David"]}); { ... Read More

Ignore NULL and UNDEFINED values while running a MongoDB query

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:45:49

1K+ Views

To ignore NULL and UNDEFINED values, use $ne in MongoDB. Let us create a collection with documents −> db.demo35.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e175e42cfb11e5c34d898d0") } > db.demo35.insertOne({"Name":null}); {    "acknowledged" : true, 9    "insertedId" : ObjectId("5e175e46cfb11e5c34d898d1") } > db.demo35.insertOne({"Name":"Bob"}); {    "acknowledged" : true,   ... Read More

How to compare attributes of different objects in MongoDB object array?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:43:58

614 Views

To compare attributes, use $let along with $indexOfArray. Let us first create a collection with documents −> db.demo366.insertOne( ...    { ... ...       "Name" : "Chris", ...       "details" : [ ...       { ...          "Id" : "John1", ... ... Read More

Advertisements