Found 1349 Articles for MongoDB

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

Query MongoDB collection starting with _?

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

133 Views

For MongoDB collection beginning with_, following is the syntax −db.createCollection(‘_yourCollectionName’);Insert query using below syntax −db.getCollection('_yourCollectionName').insertOne({"yourFieldName1":"yourValue1", "yourFieldName2":yourValue2, ............N});Let us first create a collection with documents −> db.createCollection('_testUnderscoreCollectionDemo'); { "ok" : 1 } >db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"John", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccfb4a6140b992277dae0e4") } >db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"Carol", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccfb4af140b992277dae0e5") }Following is the query to display all documents from a collection with the help of find() method −> db.getCollection('_testUnderscoreCollectionDemo').find().pretty();This will produce the following output −{    "_id" : ObjectId("5ccfb4a6140b992277dae0e4"),    "StudentFirstName" : "John",    "StudentAge" : 23 } {    "_id" : ObjectId("5ccfb4af140b992277dae0e5"),   ... Read More

How to retrieve a nested object in MongoDB?

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

870 Views

To retrieve a nested object in MongoDB, use $ operator. Let us first create a collection with documents −> db.queryNestedObject.insertOne( ...    { ...       "StudentName" : "James", ...       "StudentSubjectScore" : [ ...          {"StudentMongoDBScore":98}, ...          {"StudentCScore":92}, ...          {"StudentJavaScore":91} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf49a9dceb9a92e6aa1962") }Following is the query to display all documents from a collection with the help of find() method −> db.queryNestedObject.find().pretty();This will produce the following output −{   ... Read More

Advertisements