Found 1349 Articles for MongoDB

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

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

60 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}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176d6acfb11e5c34d898e1") }Display all documents from a collection with the help of find() method −> db.demo39.find();This will produce the following output −{ "_id" : ObjectId("5e176d54cfb11e5c34d898df"), "ProductDetails" : [ { "ProductName" : "Product-1", "ProductPrice" : 349 } ] } { "_id" : ObjectId("5e176d61cfb11e5c34d898e0"), "ProductDetails" : [ ... Read More

MongoDB query for documents matching array, irrespective of elements order

AmitDiwan
Updated on 02-Apr-2020 12:56:10

169 Views

For this, use $all in MongoDB. The $all operator in MongoDB selects the documents where the value of a field is an array that contains all the specified elements.Let us first create a collection with documents −> db.demo370.insertOne( ...    { ...       "Name" : "Chris", ...       "details" : [ ...          { ...             "Subject" : "MySQL", ...             "CountryName" : "US" ...          }, ...          { ...             ... Read More

Retrieving array values from a find query in MongoDB database

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

68 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" : true,    "insertedId" : ObjectId("5e176ad0cfb11e5c34d898db") }Display all documents from a collection with the help of find() method −> db.demo38.find();This will produce the following output −{ "_id" : ObjectId("5e176abccfb11e5c34d898d9"), "ClientDetails" : [ { "ClientId" : 101, "ClientName" : "Chris" } ] } { "_id" : ObjectId("5e176ac7cfb11e5c34d898da"), "ClientDetails" : [ { ... Read More

MongoDB query to convert string to int?

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" : ObjectId("5e57e2ee2ae06a1609a00aef") }Display all documents from a collection with the help of find() method −> db.demo369.find();This will produce the following output −{ "_id" : ObjectId("5e57e2e32ae06a1609a00aed"), "Price" : "1000000" } { "_id" : ObjectId("5e57e2e92ae06a1609a00aee"), "Price" : "1747864" } { "_id" : ObjectId("5e57e2ee2ae06a1609a00aef"), "Price" : "19548575" }Following is the query to convert ... Read More

How to select a specific subdocument in MongoDB?

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 collection with the help of find() method −> db.demo37.find();This will produce the following output −{ "_id" : ObjectId("5e176635cfb11e5c34d898d7"), "Details" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "David", "Age" : 23 } ] } { "_id" : ObjectId("5e17664acfb11e5c34d898d8"), "Details" : [ { "Name" : "Sam", ... Read More

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

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

170 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"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57e21a2ae06a1609a00aeb") } >db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').insertOne({"FirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57e21e2ae06a1609a00aec") }Display all documents from a collection with the help of find() method −> db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').find();This will produce the following output −{ "_id" : ObjectId("5e57e2152ae06a1609a00aea"), "FirstName" : "Chris" } { "_id" : ObjectId("5e57e21a2ae06a1609a00aeb"), ... Read More

Get the length of distinct values in an array with MongoDB

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

263 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"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e17645bcfb11e5c34d898d6") }Display all documents from a collection with the help of find() method −> db.demo36.find();This will produce the following output −{ "_id" : ObjectId("5e17643bcfb11e5c34d898d4"), "Names" : [ "Chris", "Bob" ] } { "_id" : ObjectId("5e176449cfb11e5c34d898d5"), "Names" : [ "Mike", "Sam", "Carol" ] } ... Read More

Ignore NULL and UNDEFINED values while running a MongoDB query

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

942 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,    "insertedId" : ObjectId("5e175e4bcfb11e5c34d898d2") } > db.demo35.insertOne({"Name":undefined}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e175e54cfb11e5c34d898d3") }Display all documents from a collection with the help of find() method −> db.demo35.find();This will produce the following output −{ "_id" : ObjectId("5e175e42cfb11e5c34d898d0"), "Name" : "Chris" } { "_id" : ObjectId("5e175e46cfb11e5c34d898d1"), "Name" : null } ... Read More

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

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

852 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"}, ...    { "Age" : 23, "CountryName" : "UK"} ], ...    "isMarried" : false } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57e0b62ae06a1609a00ae8") } > db.demo367.insertOne( ...    { "Id" : "101", ...    "details" : [ { "Name" : "Chris", "Subject" : [ "MySQL" ] }, ... ... Read More

MongoDB query to update a MongoDB row with only the objectid

AmitDiwan
Updated on 02-Apr-2020 12:43:57

144 Views

Use UPDATE to update and SET to set the new values. Let us create a collection with documents −> db.demo34.insertOne({"StudentFirstName":"Chris", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1758b4cfb11e5c34d898cd") } > db.demo34.insertOne({"StudentFirstName":"David", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1758bdcfb11e5c34d898ce") } > db.demo34.insertOne({"StudentFirstName":"Bob", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1758c6cfb11e5c34d898cf") }Display all documents from a collection with the help of find() method −> db.demo34.find();This will produce the following output −{ "_id" : ObjectId("5e1758b4cfb11e5c34d898cd"), "StudentFirstName" : "Chris", "StudentAge" : 24 } { "_id" : ObjectId("5e1758bdcfb11e5c34d898ce"), "StudentFirstName" : "David", "StudentAge" : 23 } { "_id" : ... Read More

Advertisements