Found 1349 Articles for MongoDB

Removing empty fields from MongoDB

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

375 Views

To remove empty fields, use deleteMany(). Let us first create a collection with documents −> db.removeEmptyFieldsDemo.insertOne({"StudentName":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce92b9578f00858fb12e919") } > db.removeEmptyFieldsDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce92b9878f00858fb12e91a") } > db.removeEmptyFieldsDemo.insertOne({"StudentName":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce92b9c78f00858fb12e91b") } > db.removeEmptyFieldsDemo.insertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce92ba078f00858fb12e91c") }Following is the query to display all documents from a collection with the help of find() method −> db.removeEmptyFieldsDemo.find();This will produce the following output −{ "_id" : ObjectId("5ce92b9578f00858fb12e919"), "StudentName" : "" } { "_id" : ObjectId("5ce92b9878f00858fb12e91a"), "StudentName" : "Chris" ... Read More

Creating an index on a nested MongoDB field?

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

1K+ Views

You can use dot(.) notation for this. Let us first create a collection with documents −> db.createIndexOnNestedFieldDemo.insertOne(    {"UserDetails":{"UserPersonalDetails":{"UserFirstName":"John", "UserLastName":"Smith"}}});    {       "acknowledged" : true,       "insertedId" : ObjectId("5ce929c778f00858fb12e916")    } > > db.createIndexOnNestedFieldDemo.insertOne( {"UserDetails":{"UserPersonalDetails":{"UserFirstName":"Chris", "UserLastName":"Brown"}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce929d678f00858fb12e917") } > db.createIndexOnNestedFieldDemo.insertOne( {"UserDetails":{"UserPersonalDetails":{"UserFirstName":"David", "UserLastName":"Miller"}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce929e378f00858fb12e918") }Following is the query to display all documents from a collection with the help of find() method −> db.createIndexOnNestedFieldDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ce929c778f00858fb12e916"),    "UserDetails" : {       ... Read More

Find documents where all elements of an array have a specific value in MongoDB?

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

92 Views

You can use find() for this. Let us first create a collection with documents −> db.findDocumentsDemo.insertOne(    {       _id: 101,       "ProductDetails": [          { "ProductValue":100 },          { "ProductValue":120 }       ]    } ); { "acknowledged" : true, "insertedId" : 101 } > db.findDocumentsDemo.insertOne(    {       _id: 102,       "ProductDetails": [          { "ProductValue":120},          { "ProductValue":120 },          { "ProductValue":120 }       ]    } ); { "acknowledged" ... Read More

Perform MongoDB array concatenation to concatenate records

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

88 Views

For array concatenation, use $concatArrays operator. Let us first create a collection with documents −>db.arrayConcatenationDemo.insertOne({"TeacherName":["Chris", "Robert"], "StudentName":["Mike", "Sam"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce921c078f00858fb12e911") }Following is the query to display all documents from a collection with the help of find() method −> db.arrayConcatenationDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ce921c078f00858fb12e911"),    "TeacherName" : [       "Chris",       "Robert"    ],    "StudentName" : [       "Mike",       "Sam"    ] }Following is the query for array concatenation −> db.arrayConcatenationDemo.aggregate([    { "$project": {       ... Read More

MongoDB query select and display only a specific field from the document?

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

2K+ Views

Let us first create a collection with documents −> db.querySelectDemo.insertOne({UserId:100, UserName:"Chris", UserAge:25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce90eb478f00858fb12e90e") } > db.querySelectDemo.insertOne({UserId:101, UserName:"Robert", UserAge:26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce90ec578f00858fb12e90f") } > db.querySelectDemo.insertOne({UserId:103, UserName:"David", UserAge:27}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce90ed478f00858fb12e910") }Following is the query to display all documents from a collection with the help of find() method −> db.querySelectDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ce90eb478f00858fb12e90e"),    "UserId" : 100,    "UserName" : "Chris",    "UserAge" : 25 } {    "_id" : ObjectId("5ce90ec578f00858fb12e90f"),    "UserId" : 101, ... Read More

MongoDB query to update an array element matching a condition using $push?

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

354 Views

Let us first create a collection with documents −> db.updateArrayElementDemo.insertOne(    {       "UserDetails":       [          {             "UserName":"Chris",             "UserAge":23          }       ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce9029378f00858fb12e90d") }Following is the query to display all documents from a collection with the help of find() method −> db.updateArrayElementDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ce9029378f00858fb12e90d"),    "UserDetails" : [       {       ... Read More

MongoDB query to find documents having two values in an array conforming to multiple criteria?

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

253 Views

For this, use $elemMatch operator. Let us first create a collection with documents −> db.findDocumentsHaving2Demo.insertOne(    {_id : 101, Values: [78, 98]} ); { "acknowledged" : true, "insertedId" : 101 } > db.findDocumentsHaving2Demo.insertOne(    {_id :102, Values : [89, 102]} ); { "acknowledged" : true, "insertedId" : 102 }Following is the query to display all documents from a collection with the help of find() method −> db.findDocumentsHaving2Demo.find().pretty();This will produce the following output −{ "_id" : 101, "Values" : [ 78, 98 ] } { "_id" : 102, "Values" : [ 89, 102 ] }Following is the query to find documents ... Read More

Sort and Group in one MongoDB aggregation query?

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

613 Views

For sorting and grouping in a single query, use the $group operator along with aggregate framework. Let us first create a collection with documents −> db.sortAndGroupDemo.insertOne({    Price :40,    Product: 10 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce8f2bc78f00858fb12e907") } > db.sortAndGroupDemo.insertOne({    Price :100,    Product: 10 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce8f2bc78f00858fb12e908") } > db.sortAndGroupDemo.insertOne({    Price :90,    Product: 20 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce8f2bc78f00858fb12e909") } > db.sortAndGroupDemo.insertOne({    Price :200,    Product: 10 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce8f2bc78f00858fb12e90a") } ... Read More

MongoDB query to remove empty objects in an object-array?

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

689 Views

You can use $pull operator for this. Let us first create a collection with documents. Here, we have also added an empty object −> db.removeEmptyObjectsDemo.insertOne(    {       "_id" :101,       "LoginDate" :new ISODate(),       "UserDetails" : [          {             "UserName" : "John"          },          {          },          {             "UserName" : "Sam"          }       ]    } ); { ... Read More

How to return documents of a collection without objectId in MongoDB?

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

2K+ Views

To return documents of a collection without objectId, set _id:0. Let us first create a collection with documents −> db.returnDocumentWithoutObjectId.insertOne({"Name":"Carol", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce8ba6c78f00858fb12e8fa") } > db.returnDocumentWithoutObjectId.insertOne({"Name":"Sam", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce8ba6d78f00858fb12e8fb") } > db.returnDocumentWithoutObjectId.insertOne({"Name":"John", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce8ba6f78f00858fb12e8fc") }Following is the query to display all documents from a collection with the help of find() method −> db.returnDocumentWithoutObjectId.find();This will produce the following output −{ "_id" : ObjectId("5ce8ba6c78f00858fb12e8fa"), "Name" : "Carol", "Age" : 25 } { "_id" : ObjectId("5ce8ba6d78f00858fb12e8fb"), "Name" : "Sam", "Age" : ... Read More

Advertisements