Found 1349 Articles for MongoDB

How to remove a specific element from array in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 11:41:40

594 Views

To remove a specific element, use $pull. Let us create a collection with documents −> db.demo125.insertOne({"ListOfNames":["John", "Chris", "Bob", "David", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f304068e7f832db1a7f55") }Display all documents from a collection with the help of find() method −> db.demo125.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e2f304068e7f832db1a7f55"),    "ListOfNames" : [       "John",       "Chris",       "Bob",       "David",       "Carol"    ] }Following is the query to remove a specific element from array in MongoDB −> db.demo125.update( ... { }, ... { $pull: ... Read More

How to add new item in nested array with MongoDB?

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

501 Views

For this, use find() along with update(). Let us create a collection with documents −> db.demo124.insertOne( ...    { ...       "Name" : "John", ...       "Id" : 101, ...       "ProjectDetails" : [{ ...          "ProjectName1" : "Online Book", ...          "ProjectName2" : "Online Banking" ...    }, { ...          "ProjectName1" : "Online Library Management System", ...          "ProjectName2" : "School Management System" ...       }] ...    } ... ); {    "acknowledged" : true,    "insertedId" ... Read More

Search for documents with similar arrays in MongoDB and order by similarity value

AmitDiwan
Updated on 31-Mar-2020 11:35:29

185 Views

Let us create a collection with documents −> db.demo123.insertOne({"ListOfSubject":['MySQL', 'MongoDB', 'Java']}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f24ac140daf4c2a3544b8") } > db.demo123.insertOne({"ListOfSubject":['Python', 'MongoDB', 'C']}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f24cd140daf4c2a3544b9") } > db.demo123.insertOne({"ListOfSubject":['MySQL', 'MongoDB', 'C++']}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f24ce140daf4c2a3544ba") }Display all documents from a collection with the help of find() method −> db.demo123.find();This will produce the following output −{ "_id" : ObjectId("5e2f24ac140daf4c2a3544b8"), "ListOfSubject" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e2f24cd140daf4c2a3544b9"), "ListOfSubject" : [ "Python", "MongoDB", "C" ] } { "_id" : ObjectId("5e2f24ce140daf4c2a3544ba"), "ListOfSubject" : [ "MySQL", "MongoDB", ... Read More

Get distinct levels of array field in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 11:33:10

172 Views

To get distinct levels of array field, use $addToSet in MongoDB. Let us create a collection with documents −> db.demo122.insertOne({"ListOfValues":[100, 10]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f20f1140daf4c2a3544b6") } > db.demo122.insertOne({"ListOfValues":[240, 10]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f20f7140daf4c2a3544b7") }Display all documents from a collection with the help of find() method −> db.demo122.find();This will produce the following output −{ "_id" : ObjectId("5e2f20f1140daf4c2a3544b6"), "ListOfValues" : [ 100, 10 ] } { "_id" : ObjectId("5e2f20f7140daf4c2a3544b7"), "ListOfValues" : [ 240, 10 ] }Following is the query to get distinct levels of array field in MongoDB −> db.demo122.aggregate([ ...   ... Read More

MongoDB query to implement aggregate function

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

145 Views

Let us first create a collection with documents −> db.demo121.insertOne( ...    { ...       "Id" : 101, ...       "Details" : [ ...          { ...             "SubjectId" : "1", ...             "SubjectName" : "MongoDB", ...             "Score" : 76 ...          }, ...          { ...             "SubjectId" : "2", ...             "SubjectName" : "MySQL", ...         ... Read More

MongoDB query for ranking / search count?

AmitDiwan
Updated on 31-Mar-2020 11:27:13

363 Views

For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo120.insertOne( ...    { ...       'Name': 'Chris', ...       'Subjects': [ 'MySQL', 'MongoDB', 'Java', 'Python' ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f11aed8f64a552dae6365") } > db.demo120.insertOne( ...    { ...       'Name': 'Bob', ...       'Subjects': [ 'C', 'MongoDB' ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f11afd8f64a552dae6366") }Display all documents from a collection with the help of find() method −> db.demo120.find();This will produce ... Read More

Prevent duplicates of multiple fields with index in MongoDB

AmitDiwan
Updated on 31-Mar-2020 08:52:18

406 Views

To prevent duplicates of multiple fields, use ensureIndex() and set unique:true. Let us create a collection with documents −> db.demo272.ensureIndex({"FirstName":1, "Subject":1}, {unique:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo272.insertOne({"FirstName":"Chris", "Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48232a1627c0c63e7dbabf") } > db.demo272.insertOne({"FirstName":"Chris", "Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48232f1627c0c63e7dbac0") } > db.demo272.insertOne({"FirstName":"David", "Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48233a1627c0c63e7dbac1") } > db.demo272.insertOne({"FirstName":"Chris", "Subject":"MySQL"}); 2020-02-15T22:28:55.137+0530 E QUERY    [js] WriteError: E11000 duplicate key error collection: test.demo272 index: FirstName_1_Subject_1 dup key: { : ... Read More

MongoDB query to increment a specific value using custom variable

AmitDiwan
Updated on 31-Mar-2020 08:50:12

386 Views

Set a custom variable and use update() along with $inc to increment. Let us create a collection with documents −> db.demo271.insertOne({"Marks":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4821211627c0c63e7dbabc") } > db.demo271.insertOne({"Marks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4821241627c0c63e7dbabd") } > db.demo271.insertOne({"Marks":72}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48212b1627c0c63e7dbabe") }Display all documents from a collection with the help of find() method −> db.demo271.find();This will produce the following output −{ "_id" : ObjectId("5e4821211627c0c63e7dbabc"), "Marks" : 56 } { "_id" : ObjectId("5e4821241627c0c63e7dbabd"), "Marks" : 78 } { "_id" : ObjectId("5e48212b1627c0c63e7dbabe"), "Marks" : 72 }Following is the query ... Read More

Select multiple values with MongoDB OR operator

AmitDiwan
Updated on 31-Mar-2020 08:48:45

598 Views

Let us first create a collection with documents −> db.demo270.insertOne({"ClientName":"Chirs", "Age":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481e371627c0c63e7dbab8") } > db.demo270.insertOne({"ClientName":"David", "Age":31}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481e3d1627c0c63e7dbab9") } > db.demo270.insertOne({"ClientName":"Bob", "Age":31}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481e431627c0c63e7dbaba") } > db.demo270.insertOne({"ClientName":"Carol", "Age":36}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481e491627c0c63e7dbabb") }Display all documents from a collection with the help of find() method −> db.demo270.find();This will produce the following output −{ "_id" : ObjectId("5e481e371627c0c63e7dbab8"), "ClientName" : "Chirs", "Age" : 34 } { "_id" : ObjectId("5e481e3d1627c0c63e7dbab9"), "ClientName" : "David", "Age" : 31 } ... Read More

Select special fields rather than all in MongoDB

AmitDiwan
Updated on 31-Mar-2020 08:46:24

84 Views

For this, simply use find(). Set the fields you don’t want to select to 0. Let us create a collection with documents −> db.demo269.insertOne({StudentId:101, StudentSubject:"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481caa1627c0c63e7dbab4") } > db.demo269.insertOne({StudentId:102, StudentSubject:"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481cb11627c0c63e7dbab5") } > db.demo269.insertOne({StudentId:103, StudentSubject:"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481cb21627c0c63e7dbab6") } > db.demo269.insertOne({StudentId:104, StudentSubject:"C"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481cb21627c0c63e7dbab7") }Display all documents from a collection with the help of find() method −> db.demo269.find();This will produce the following output −{ "_id" : ObjectId("5e481caa1627c0c63e7dbab4"), "StudentId" : 101, "StudentSubject" ... Read More

Advertisements