Remove Entire Data from a MongoDB Collection

AmitDiwan
Updated on 01-Apr-2020 07:41:37

228 Views

To remove, use remove() in MongoDB. Let us create a collection with documents −> db.demo309.insertOne({ "details":[ { "Name":"Chris" }, { "Name":"David" }, { "Name":"Adam" } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4eb71af8647eb59e562040") } > db.demo309.insertOne({ "details":[ { "Name":"David" }, { "Name":"Mike" }, { "Name":"Bob" } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4eb7cbf8647eb59e562041") }Display all documents from a collection with the help of find() method −> db.demo309.find();This will produce the following output −{ "_id" : ObjectId("5e4eb71af8647eb59e562040"), "details" : [ { "Name" : "Chris" }, { "Name" : "David" }, { "Name" ... Read More

Retrieve a Subset of Fields from MongoDB

AmitDiwan
Updated on 01-Apr-2020 07:39:51

687 Views

To retried a subset of fields, use dot notation in find(). Let us create a collection with documents −> db.demo307.insertOne({ ...   "CleintId":101, ...   "ClientDetails":{"ClientFirstName":"Chris", "Age":34}, ...   "ClientCountryName":"US" ...} ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4eab88f8647eb59e56203c") } > db.demo307.insertOne({ ...   "CleintId":102, ...   "ClientDetails":{"ClientFirstName":"David", "Age":31}, ...   "ClientCountryName":"UK" ...} ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4eab97f8647eb59e56203d") }Display all documents from a collection with the help of find() method −>  db.demo307.find();This will produce the following output −{ "_id" : ObjectId("5e4eab88f8647eb59e56203c"), "CleintId" : 101, "ClientDetails" : { "ClientFirstName" : "Chris", "Age" : 34 ... Read More

Fetch All Documents from MongoDB Collection in a Beautified Form

AmitDiwan
Updated on 01-Apr-2020 07:37:27

372 Views

To fetch documents, use find() in MongoDB. With that, to format the resultant documents, use pretty().Let us create a collection with documents −> db.demo306.insertOne({"Name":"Robert", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea7c6f8647eb59e562038") } > db.demo306.insertOne({"Name":"David", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea7cdf8647eb59e562039") } > db.demo306.insertOne({"Name":"Mike", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea7d4f8647eb59e56203a") } > db.demo306.insertOne({"Name":"Carol", "Age":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea7dcf8647eb59e56203b") }Display all documents from a collection with the help of find() method −> db.demo306.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e4ea7c6f8647eb59e562038"),    "Name" : ... Read More

Get Child of a MongoDB Collection by Key

AmitDiwan
Updated on 01-Apr-2020 07:35:48

1K+ Views

To get the child of a collection in MongoDB, use find(). Let us create a collection with documents −> db.demo305.insertOne( ...   { ...      _id: 101, ...      FirstName : 'Chris', ...      details : { ...         "0":"102", ...         "1":"10001" ...      } ...   } ...); { "acknowledged" : true, "insertedId" : 101 } > db.demo305.insertOne( ...   { ...      _id: 102, ...      FirstName : 'David', ...      details : { ...         "0":"103", ...         "1":"10002" ...      } ...   } ...); { "acknowledged" : true, "insertedId" : 102 }Display all documents from a collection with the help of find() method −> db.demo305.find();This will produce the following output −{ "_id" : 101, "FirstName" : "Chris", "details" : { "0" : "102", "1" : "10001" } } { "_id" : 102, "FirstName" : "David", "details" : { "0" : "103", "1" : "10002" } }Following is the query to get the child of a MongoDB collection by the key −> db.demo305.find({_id:102},{'details.0':1});This will produce the following output −{ "_id" : 102, "details" : { "0" : "103" } }

Find Values with OR Operator in MongoDB and Format the Result

AmitDiwan
Updated on 01-Apr-2020 07:32:22

145 Views

Use $or operator to fetch values and to format the result, use “pretty()”. Let us create a collection with documents −> db.demo304.insertOne({"StudentName":"Chris", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea3ccf8647eb59e562034") } > db.demo304.insertOne({"StudentName":"David", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea3d7f8647eb59e562035") } > db.demo304.insertOne({"StudentName":"Mike", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea3e2f8647eb59e562036") } > db.demo304.insertOne({"StudentName":"Carol", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea3eef8647eb59e562037") }Display all documents from a collection with the help of find() method −> db.demo304.find();This will produce the following output −{ "_id" : ObjectId("5e4ea3ccf8647eb59e562034"), "StudentName" : "Chris", "StudentAge" : 23 ... Read More

MongoDB Query to Get Distinct Firstname Values from Documents

AmitDiwan
Updated on 01-Apr-2020 07:29:36

330 Views

For distinct values, use distinct(). Let us create a collection with documents −> db.demo303.insertOne({FirstName:"Chris", LastName:"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea0f6f8647eb59e56202f") } > db.demo303.insertOne({FirstName:"John", LastName:"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea104f8647eb59e562030") } > db.demo303.insertOne({FirstName:"Chris", LastName:"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea10df8647eb59e562031") } > db.demo303.insertOne({FirstName:"John", LastName:"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea121f8647eb59e562032") } > db.demo303.insertOne({FirstName:"David", LastName:"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ea136f8647eb59e562033") }Display all documents from a collection with the help of find() method −> db.demo303.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e4ea0f6f8647eb59e56202f"), ... Read More

Field Selection in MongoDB Query Using Dot Notation

AmitDiwan
Updated on 01-Apr-2020 07:27:37

506 Views

For this, use dot notation for field selection in MongoDB find(). Let us create a collection with documents −> db.demo302.insertOne({"Id":101, "details":[{"Name":"Chris", Age:21, "Subject":"MySQL"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d746f5d93261e4bc9ea52") } > db.demo302.insertOne({"Id":102, "details":[{"Name":"Bob", Age:23, "Subject":"MongoDB"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d74815d93261e4bc9ea53") } > db.demo302.insertOne({"Id":103, "details":[{"Name":"David", Age:20, "Subject":"Java"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d74955d93261e4bc9ea54") }Display all documents from a collection with the help of find() method −> db.demo302.find();This will produce the following output −{ "_id" : ObjectId("5e4d746f5d93261e4bc9ea52"), "Id" : 101, "details" : [ { "Name" : "Chris", "Age" : 21, "Subject" : ... Read More

MongoDB Query to Change Order of Array Elements

AmitDiwan
Updated on 01-Apr-2020 07:26:34

620 Views

Use swap concept to change the order of array elements. Let us create a collection with documents −> db.demo301.insertOne({"Name":["Chris", "David", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d6ff55d93261e4bc9ea51") }Display all documents from a collection with the help of find() method −> db.demo301.find();This will produce the following output −{ "_id" : ObjectId("5e4d6ff55d93261e4bc9ea51"), "Name" : [ "Chris", "David", "Bob" ] }Following is the query to change the order of array elements −> db.demo301.find({}, { Name : 1 }).forEach(function(n) { ...   var t = n.Name[0]; ...   n.Name[0] = n.Name[1]; ...   n.Name[1] = t; ...   db.demo301.update({ _id: n._id ... Read More

MongoDB Exact Array Matching

AmitDiwan
Updated on 01-Apr-2020 07:24:30

407 Views

For exact array matching, simply use find() in MongoDB. Let us create a collection with documents −> db.demo300.insertOne({"Values":[100, 200, 400]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d69d05d93261e4bc9ea4d") } > db.demo300.insertOne({"Values":[500, 700, 900, 1000]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d69dd5d93261e4bc9ea4e") } > db.demo300.insertOne({"Values":[340, 670, 450, 500]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d69eb5d93261e4bc9ea4f") }Display all documents from a collection with the help of find() method −> db.demo300.find();This will produce the following output −{ "_id" : ObjectId("5e4d69d05d93261e4bc9ea4d"), "Values" : [ 100, 200, 400 ] } { "_id" : ObjectId("5e4d69dd5d93261e4bc9ea4e"), "Values" : [ 500, 700, 900, ... Read More

MongoDB Query to Access an Object in an Array

AmitDiwan
Updated on 01-Apr-2020 07:23:32

564 Views

To access an object in an array, use dot notation. Let us create a collection with documents −> db.demo299.insertOne( ...   { ...      "id":100, ...      "Name":"Robert", ...      "details":[ ...         { ...            "SubjectName":["C++", "Python"] ...         }, ...         { ...            "SubjectName":["Spring", "Hibernate"] ...         } ...      ] ...   } ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d685a5d93261e4bc9ea4b") } > > > db.demo299.insertOne( ...   { ... ... Read More

Advertisements