Found 1659 Articles for Big Data Analytics

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

853 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

MongoDB aggregation framework to sort by length of array?

AmitDiwan
Updated on 02-Apr-2020 12:42:50

1K+ Views

To sort by length of array, use aggregate(). Before that, get the count of records in an arrayusing $sum. Let us create a collection with documents> db.demo33.insertOne({"ListOfStudent":["Chris", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e17556ccfb11e5c34d898ca") } > db.demo33.insertOne({"ListOfStudent":["David", "Adam", "Mike"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e17557acfb11e5c34d898cb") } > db.demo33.insertOne({"ListOfStudent":["Carol", "Sam", "John", "Robert"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1755a3cfb11e5c34d898cc") }Display all documents from a collection with the help of find() method −> db.demo33.find();This will produce the following output −{ "_id" : ObjectId("5e17556ccfb11e5c34d898ca"), "ListOfStudent" : [ "Chris", "Bob" ] } { "_id" : ObjectId("5e17557acfb11e5c34d898cb"), ... Read More

Looping MongoDB collection for sorting

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

80 Views

To sort records in a collection, use sort() in MongoDB. Let us create a collection with documents −> db.demo32.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e175158cfb11e5c34d898c5") } > db.demo32.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e17515ccfb11e5c34d898c6") } > db.demo32.insertOne({"Name":"Adam"}); {    "acknowledged" : true,    "insertedId" : Object0Id("5e175160cfb11e5c34d898c7") } > db.demo32.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e175165cfb11e5c34d898c8") } > db.demo32.insertOne({"Name":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e17517bcfb11e5c34d898c9") }Display all documents from a collection with the help of find() method −> db.demo32.find();This will produce the following output −{ "_id" : ObjectId("5e175158cfb11e5c34d898c5"), "Name" ... Read More

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

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

499 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

How to force MongoDB to use the BasicCursor instead of an index?

AmitDiwan
Updated on 02-Apr-2020 12:39:38

128 Views

To avoid using index, use hint() in MongoDB. Let us create a collection with documents −> db.demo31.createIndex({"StudentFirstName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo31.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e174f8fcfb11e5c34d898c1") } > db.demo31.insertOne({"StudentFirstName":"Jace"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e174f97cfb11e5c34d898c2") } > db.demo31.insertOne({"StudentFirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e174f9ccfb11e5c34d898c3") } > db.demo31.insertOne({"StudentFirstName":"James"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e174fa0cfb11e5c34d898c4") }Display all documents from a collection with the help of find() method −> db.demo31.find();This will produce the following ... Read More

Advertisements