Retrieve Array Values from a Find Query in MongoDB

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

105 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

Different Status States of REPL in Java 9

raja
Updated on 02-Apr-2020 12:50:55

320 Views

REPL stands for Read-Evaluate-Print-Loop. It holds some states, and each statement in JShell has a state. This state denies the execution status of snippets and variables. It can be determined by the results of the eval() method of JShell instance, which evaluates the code.There are seven different status states listed below.DROPPED: The snippet is inactive.NONEXISTENT: The snippet is inactive because it does not yet exist.OVERWRITTEN: The snippet is inactive because it has been replaced by a new snippet.RECOVERABLE_DEFINED: The snippet is a declaration snippet with potentially recoverable unresolved references or other issues in its body.RECOVERABLE_NOT_DEFINED: The snippet is a declaration snippet with ... 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

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

221 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

MongoDB Query to Find Based on True or False Values

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

Get Length of Distinct Values in an Array with MongoDB

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

348 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 in MongoDB Query

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,    "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

Compare Attributes of Different Objects in MongoDB Object Array

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

624 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", ...          "value" : "test" ...       }, ...       { ...          "Id" : "John2", ...          "value" : 18 ...       }, ...       { ...          "Id" : ... Read More

Update MongoDB Row Using Only ObjectId

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

209 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