Found 1659 Articles for Big Data Analytics

What does createdCollectionAutomatically mean in MongoDB?

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

167 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

106 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

859 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

1K+ 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

122 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

276 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

284 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

Display distinctly ordered MongoDB record with skip and limit?

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

697 Views

You can work with aggregate framework and use $sort, $skip and $limit to display distinctly ordered records with skip and also set the limit. Let us first create a collection with documents> db.orderedDistinctDemo.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccfb8e0140b992277dae0e9") } > db.orderedDistinctDemo.insertOne({"Name":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccfb8e5140b992277dae0ea") } > db.orderedDistinctDemo.insertOne({"Name":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccfb8e7140b992277dae0eb") } > db.orderedDistinctDemo.insertOne({"Name":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccfb8ea140b992277dae0ec") } > db.orderedDistinctDemo.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccfb8ee140b992277dae0ed") } > db.orderedDistinctDemo.insertOne({"Name":"Carol"}); {    "acknowledged" : true,    "insertedId" ... Read More

How to delete a table from MongoDB database?

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

380 Views

Use drop() to delete a table. Following is the syntax −db.yourCollectionName.drop();Let us first create a collection with documents −> db.deleteTableDemo.insertOne({"Name":"Chris", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccfb705140b992277dae0e6") } > db.deleteTableDemo.insertOne({"Name":"Carol", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccfb70c140b992277dae0e7") } > db.deleteTableDemo.insertOne({"Name":"David", "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccfb714140b992277dae0e8") }Following is the query to display all documents from a collection with the help of find() method −> db.deleteTableDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ccfb705140b992277dae0e6"),    "Name" : "Chris",    "Age" : 23 } {    "_id" : ObjectId("5ccfb70c140b992277dae0e7"),   ... Read More

Advertisements