Found 1659 Articles for Big Data Analytics

Display databases in MongoDB

AmitDiwan
Updated on 31-Mar-2020 08:11:23

578 Views

To display number of databases in MongoDB, you need to create atleast one document in a database.Let’s say, you have created a database, but did not added any document in it. Then in the list of databases that particular database won’t be visible.Following is the query to create a database −> use app; switched to db appFollowing is the query to display all databases −> show dbs;This will produce the following output. The new database “app” won’t be visible since we haven’t added atleast one document in it −admin                0.002GB business     ... Read More

MongoDB query to match and remove element from an array?

AmitDiwan
Updated on 31-Mar-2020 08:04:39

346 Views

To match and remove element(s) , use MongoDB $pullAll. Let us first create a collection with documents −> db.removeElementsDemo.insertOne({"ListOfNames":["Mike", "Sam", "David", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e071e5a25ddae1f53b62203") }Following is the query to display all documents from a collection with the help of find() method −> db.removeElementsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e071e5a25ddae1f53b62203"),       "ListOfNames" : [          "Mike",          "Sam",          "David",          "Carol"    ] }Here is the query to match and remove element(s) from an array ... Read More

How to find by id in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 08:02:26

11K+ Views

To find by id in MongoDB, use the find() method as in the below syntax −db.findByIdDemo.find({"_id" :yourObjectId});To understand the above syntax, let us create a collection with documents −> db.findByIdDemo.insertOne({"Value":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e07158925ddae1f53b621fc") } > db.findByIdDemo.insertOne({"Value":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e07158c25ddae1f53b621fd") } > db.findByIdDemo.insertOne({"Value":1000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e07159125ddae1f53b621fe") }Following is the query to display all documents from a collection with the help of find() method −> db.findByIdDemo.find();This will produce the following output −"_id" : ObjectId("5e07158925ddae1f53b621fc"), "Value" : 10 } { "_id" : ObjectId("5e07158c25ddae1f53b621fd"), "Value" : ... Read More

MongoDB query to fetch array values

AmitDiwan
Updated on 31-Mar-2020 08:00:16

449 Views

Use find() along with $elemMatch to fetch array values. Let us first create a collection with documents −> db.fetchingArrayValuesDemo.insertOne( ... { ...    "StudentName": "David", ...    "StudentDetails": [ ...       { ...          "FatherName": "Bob", ...          "CountryName": "US", ... ...          "Favourite": [ ...             { ...                "Teacher": "DAVID", ...                "Subject": [ ...                   "MySQL", ...         ... Read More

MongoDB indexes not working when executing $elemMatch?

AmitDiwan
Updated on 31-Mar-2020 07:54:47

359 Views

To implement indexes correctly with $elemMatch, you need to use the concept of explain(). Let us first create a collection with documents −> db.workingOfIndexesDemo.createIndex({"Information.StudentDetails.StudentName":1}, { sparse : true, background : true } ); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"Chris"}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f94825ddae1f53b621f7") } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"David"}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f94f25ddae1f53b621f8") } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"Mike"}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f95325ddae1f53b621f9") }Following is the query to display all documents from a collection with the help ... Read More

Implement a MongoDB $cond field in a projection pipeline based on the presence or absence of a field?

AmitDiwan
Updated on 31-Mar-2020 07:48:46

198 Views

For this, you can use $cond along with $anyElementTrue. NULL values (absence of a field) would evaluate to FALSE. With that, an empty array also returns FALSE with $ anyElementTrue.Let us first create a collection with documents −> db.presenceDemo.insertOne({"StudentName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f70c25ddae1f53b621f3") } > db.presenceDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f71425ddae1f53b621f4") } > db.presenceDemo.insertOne({"StudentName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f71825ddae1f53b621f5") } > db.presenceDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f71e25ddae1f53b621f6") }Following is the query to display all documents from a collection with the help of find() ... Read More

How can I search a collection to find a nested value in one of its documents in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 07:43:48

77 Views

For this, use double underscore( __) in find(). Let us first create a collection with documents −> db.nestedDemo.insertOne({"Information":{"__StudentName":"John Smith"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f39125ddae1f53b621f0") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"John Doe"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f39e25ddae1f53b621f1") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"Chris Brown"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f3a625ddae1f53b621f2") }Following is the query to display all documents from a collection with the help of find() method −> db.nestedDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e06f39125ddae1f53b621f0"),    "Information" : {       "__StudentName" : "John Smith"    } } {    "_id" ... Read More

Find MongoDB document with array containing the maximum occurrence of a specific value

AmitDiwan
Updated on 31-Mar-2020 07:40:57

149 Views

For this, you can use aggregate(). Let us first create a collection with documents −> db.countOccurrencesDemo.insertOne({"ListOfValues":[65, 87, 89, 65, 67, 87, 87, 87]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ef9325ddae1f53b621eb") } > db.countOccurrencesDemo.insertOne({"ListOfValues":[102, 65, 87, 65, 89, 65, 89, 65, 89, 65]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06efaa25ddae1f53b621ec") }Following is the query to display all documents from a collection with the help of find() method −> db.countOccurrencesDemo.find();This will produce the following output −{ "_id" : ObjectId("5e06ef9325ddae1f53b621eb"), "ListOfValues" : [ 65, 87, 89, 65, 67, 87, 87, 87 ] } { "_id" : ObjectId("5e06efaa25ddae1f53b621ec"), "ListOfValues" : ... Read More

Update only a single document in MongoDB

AmitDiwan
Updated on 31-Mar-2020 07:38:42

131 Views

To update only a single document in a collection. use updateOne(). Let us first create a collection with documents −> db.updateOneDemo.insertOne({"StudentId":1, "StudentFirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ed3725ddae1f53b621e8") } > db.updateOneDemo.insertOne({"StudentId":2, "StudentFirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ed3825ddae1f53b621e9") } > db.updateOneDemo.insertOne({"StudentId":1, "StudentFirstName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ed3825ddae1f53b621ea") }Following is the query to display all documents from a collection with the help of find() method −> db.updateOneDemo.find();This will produce the following output −{ "_id" : ObjectId("5e06ed3725ddae1f53b621e8"), "StudentId" : 1, "StudentFirstName" : "Chris" } { "_id" : ObjectId("5e06ed3825ddae1f53b621e9"), "StudentId" : 2, "StudentFirstName" ... Read More

Fetch specific field values in MongoDB

AmitDiwan
Updated on 31-Mar-2020 07:36:50

924 Views

To fetch specific field values, use $in operator. The $in selects the documents where the value of a field equals any value in the specified array.Let us first create a collection with documents −> db.indexesDemo.createIndex({"StudentFirstName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.indexesDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06de4d25ddae1f53b621dd") } > db.indexesDemo.insertOne({"StudentFirstName":"Chris", "StudentLastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06de5825ddae1f53b621de") } > db.indexesDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06de6725ddae1f53b621df") } > db.indexesDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller"}); {    "acknowledged" : true, ... Read More

Advertisements