Found 1661 Articles for Big Data Analytics

MongoDB query to get record beginning with specific element from an array?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

225 Views

You can use dot(.) notation along with array index to get record beginning with specific element. Let us first create a collection with documents −>db.arrayStartsWithElementDemo.insertOne({"PlayerName":"Chris", "PlayerScore":[780, 9000, 456, 789, 987]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd29fed345990cee87fd889") } >db.arrayStartsWithElementDemo.insertOne({"PlayerName":"Robert", "PlayerScore":[890, 670, 890, 54646, 42424]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2a00c345990cee87fd88a") } >db.arrayStartsWithElementDemo.insertOne({"PlayerName":"David", "PlayerScore":[909090, 896555, 3344433, 78900]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2a025345990cee87fd88b") }Following is the query to display all documents from a collection with the help of find() method −> db.arrayStartsWithElementDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd29fed345990cee87fd889"),   ... Read More

Aggregate a $slice to get an element in exact position from a nested array in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

204 Views

You can use aggregate framework for this. Let us first create a collection with documents −>db.exactPositionDemo.insertOne({"StudentName":"John", "StudentScores":[78, 98, 56, 45, 89]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd29a1c345990cee87fd883") }Following is the query to display all documents from a collection with the help of find() method −> db.exactPositionDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd29a1c345990cee87fd883"),    "StudentName" : "John",    "StudentScores" : [       78,       98,       56,       45,       89    ] }Case 1 − Query to aggregate a $slice to get an ... Read More

What does createdCollectionAutomatically mean in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

250 Views

If a collection does not exist then MongoDB creates a collection in indexing part. The createdCollectionAutomatically tells that the operation created a collection.For our example, let us create a collection with an index −> db.createCollectionDemo.createIndex({"ClientCountryName":1});This will produce the following output −{    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Let us create a collection with documents −> db.createCollectionDemo.insertOne({"ClientName":"Larry", "ClientCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2950be3526dbddbbfb612") }Following is the query to display all documents from a collection with the help of find() method −> db.createCollectionDemo.find().pretty();This will produce the following output ... Read More

MongoDB equivalent of `select distinct(name) from collectionName where age = “25”`?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

148 Views

You can use distinct() to get the equivalent of select distinct. Let us first create a collection with documents −> db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"John", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd12759e3526dbddbbfb60b") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"Larry", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd12768e3526dbddbbfb60c") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"David", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd12773e3526dbddbbfb60d") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"Carol", "Age":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd1277ee3526dbddbbfb60e") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"Sam", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd12793e3526dbddbbfb60f") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"Larry", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd127a3e3526dbddbbfb610") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"Carol", "Age":26}); ... Read More

Delete data from a collection in MongoDB using multiple conditions?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

1K+ Views

You can use remove() for this. Let us first create a collection with documents −> db.deleteDataDemo.insertOne({_id:1, "Name":"Larry"}); { "acknowledged" : true, "insertedId" : 1 } > db.deleteDataDemo.insertOne({_id:2, "Name":"Chris"}); { "acknowledged" : true, "insertedId" : 2 } > db.deleteDataDemo.insertOne({_id:3, "Name":"Robert"}); { "acknowledged" : true, "insertedId" : 3 } > db.deleteDataDemo.insertOne({_id:4, "Name":"David"}); { "acknowledged" : true, "insertedId" : 4 } > db.deleteDataDemo.insertOne({_id:5, "Name":"Carol"}); { "acknowledged" : true, "insertedId" : 5 } > db.deleteDataDemo.insertOne({_id:6, "Name":"Sam"}); { "acknowledged" : true, "insertedId" : 6 }Following is the query to display all documents from a collection with the help of find() method −> db.deleteDataDemo.find().pretty();This will produce ... Read More

Query for multiple parameters in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

2K+ Views

To query for multiple parameters in MongoDB, you can use the dot(.) notation. Let us first create a collection with documents −> db.multipleParametersDemo.insertOne( ...    { ...       "CustomerName" : "Larry", ...       "CustomerDetails" : [ ...          { ...             "CustomerCountryName" : "US", ...             "CustomerBankName" : "HDFC", ...             "CustomerBalance" : 17363, ...          } ...       ], ...       "Purchase" : 1456, ... ...    } ... ); ... Read More

MongoDB query to pull array element from a collection?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

179 Views

Use the $pull operator to pull array element from a collection. Let us first create a collection with documents −> db.pullElementFromAnArrayDemo.insertOne( ...    { ...       "StudentScores":[89, 56, 78, 90] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd0104a588d4a6447b2e063") }Following is the query to display all documents from a collection with the help of find() method −> db.pullElementFromAnArrayDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd0104a588d4a6447b2e063"), "StudentScores" : [ 89, 56, 78, 90 ] }Following is the query to pull array element from a collection. Here, we are removing element 78 −> ... Read More

How do I add a field to existing record in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

357 Views

You can use update command to add a field to existing record. Let us first create a collection with documents −> db.addAFieldToEveryRecordDemo.insertOne({"ClientName":"Chris", "ClientAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd00e32588d4a6447b2e061") } > db.addAFieldToEveryRecordDemo.insertOne({"ClientName":"Robert", "ClientAge":36}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd00e59588d4a6447b2e062") }Following is the query to display all documents from a collection with the help of find() method −> db.addAFieldToEveryRecordDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd00e32588d4a6447b2e061"),    "ClientName" : "Chris",    "ClientAge" : 34 } {    "_id" : ObjectId("5cd00e59588d4a6447b2e062"),    "ClientName" : "Robert",    "ClientAge" : 36 }Here is the query ... Read More

Querying null value in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

362 Views

To query null value in MongoDB, use $ne operator. Let us first create a collection with documents −> db.queryingNullDemo.insertOne( ...    { ...       "StudentName":"Larry", ...       "StudentDetails": ...       { ...          "StudentAge":21, ...          "StudentSubject":"MongoDB" ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd00bec588d4a6447b2e05f") } > db.queryingNullDemo.insertOne( ...    { ...       "StudentName":"Sam", ...       "StudentDetails": ...       { ...          "StudentAge":23, ...         ... Read More

What is return type of db.collection.find() in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

The statement db.collection.find() returns the cursor of Result Set of a query by which you can iterate over the result set or print all documents.Let us first create a collection with documents −> db.findCursorDemo.insertOne({"ClientFirstName":"John", "ClientLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd00a1c588d4a6447b2e05c") } > db.findCursorDemo.insertOne({"ClientFirstName":"Carol", "ClientLastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd00a26588d4a6447b2e05d") } > db.findCursorDemo.insertOne({"ClientFirstName":"David", "ClientLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd00a33588d4a6447b2e05e") }Following is the query to display all documents from a collection with the help of find() method −> db.findCursorDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd00a1c588d4a6447b2e05c"), "ClientFirstName" : ... Read More

Advertisements