Found 1349 Articles for MongoDB

Search for a text in MongoDBs Double Nested Array?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

91 Views

Search for a text in MongoDBs Double Nested Array with the help of dot(.) notation. Let us first create a collection. Following is the query to create a collection with documents> db.doubleNestedArrayDemo.insertOne( ...    { ...       "StudentId" : "1000", ...       "StudentName" : "Larry", ...       "StudentDetails" : [ ...          { ...             "ProjectName" : "Online Banking", ...             "ProjectDetails" : [ ...                { ...               ... Read More

How to update a MongoDB document for adding a new item to an array?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

210 Views

To add a new item to an array, you can use $push operator. Let us first implement the following query to create a collection with documents:> db.updateDemo.insertOne({"StudentName":"Larry", "StudentCoreSubject":["Java", "C"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c98ba78330fd0aa0d2fe4c9") } >db.updateDemo.insertOne({"StudentName":"Robert", "StudentCoreSubject":["C++", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c98ba8b330fd0aa0d2fe4ca") } > db.updateDemo.insertOne({"StudentName":"Chris", "StudentCoreSubject":["Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c98ba9b330fd0aa0d2fe4cb") }Following is the query to display all the documents from a collection with the help of find() method> db.updateDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c98ba78330fd0aa0d2fe4c9"),    "StudentName" : "Larry",    "StudentCoreSubject" : [ ... Read More

Getting a distinct aggregation of an array field across indexes

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

71 Views

To get a distinct aggregation of an array field across indexes, let us take an example and create a collection with some documents.Following is the query to create a collection with documents> db.distinctAggregation.insertOne({"UserName":"Larry", "UserPost":["Hi", "Hello"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c98aefb330fd0aa0d2fe4c6") } > db.distinctAggregation.insertOne({"UserName":"Chris", "UserPost":["Hi", "Good Morning"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c98af0a330fd0aa0d2fe4c7") } > db.distinctAggregation.insertOne({"UserName":"Robert", "UserPost":["Awesome"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c98af1e330fd0aa0d2fe4c8") }Following is the query to display all documents from a collection with the help of find() method> db.distinctAggregation.find().pretty();This will produce the following output{    "_id" : ObjectId("5c98aefb330fd0aa0d2fe4c6"),   ... Read More

Get only the FALSE value with MongoDB query

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

157 Views

To get only the FALSE value, let us first create a collection with documents. One of the fields is isEnable that is having TRUE or FALSE values as shown below> db.translateDefinitionDemo.insertOne({"_id":10, "StudentName":"Larry", "isEnable":true}); { "acknowledged" : true, "insertedId" : 10 } > db.translateDefinitionDemo.insertOne({"_id":20, "StudentName":"Chris", "isEnable":false}); { "acknowledged" : true, "insertedId" : 20 } > db.translateDefinitionDemo.insertOne({"_id":30, "StudentName":"Robert", "isEnable":true}); { "acknowledged" : true, "insertedId" : 30 } > db.translateDefinitionDemo.insertOne({"_id":40, "StudentName":"Sam", "isEnable":false}); { "acknowledged" : true, "insertedId" : 40 } > db.translateDefinitionDemo.insertOne({"_id":50, "StudentName":"Mike", "isEnable":true}); { "acknowledged" : true, "insertedId" : 50 }Following is the query to display all the documents from a collection ... Read More

How to get all the collections from all the MongoDB databases?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

440 Views

To get all the collections from all the databases, let us first get all the databases using the following query> switchDatabaseAdmin = db.getSiblingDB("admin"); admin > allDatabaseName = switchDatabaseAdmin.runCommand({ "listDatabases": 1 }).databases;This will produce the following output[    {       "name" : "admin",       "sizeOnDisk" : 495616,       "empty" : false    },    {       "name" : "config",       "sizeOnDisk" : 98304,       "empty" : false    },    {       "name" : "local",       "sizeOnDisk" : 73728,       "empty" : false ... Read More

How to get embedded data in a MongoDB document?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

193 Views

Following is the syntax to get the embedded data in a MongoDB documentdb.yourCollectionName.find({}, {‘yourOuterKeyName.yourInnerKeyName:1}).pretty();Let us first create a collection with documents> db.embeddedCollectionDemo.insertOne( ...    { ...       "StudentName" : "Larry", ...       "StudentDetails": { ...          "Larry1234": {"ProjectName": "Student Web Tracker"}, ...          "Larry7645": {"ProjectName": "Hospital Management System"}, ...          "Larry9879": {"ProjectName": "Library Management System"}, ... ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5c98a100330fd0aa0d2fe4c5") }Following is the query to display all documents from a collection> db.embeddedCollectionDemo.find().pretty();This ... Read More

Can I get the first item in a Cursor object in MongoDB?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

496 Views

Yes, you can get the first item in a cursor object using findOne() method. Following is the syntaxdb.yourCollectionName.findOne();However, the following syntax is used if you want a single document in a cursor objectdb.yourCollectionName.findOne({yourCondition});We will first create a collection. Following is the query to create a collection with documents> db.getFirstItemDemo.insertOne({"CustomerName":"Chris", "CustomerAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c989059330fd0aa0d2fe4c1") } > db.getFirstItemDemo.insertOne({"CustomerName":"Larry", "CustomerAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c989063330fd0aa0d2fe4c2") } > db.getFirstItemDemo.insertOne({"CustomerName":"Robert", "CustomerAge":29}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c98906d330fd0aa0d2fe4c3") } > db.getFirstItemDemo.insertOne({"CustomerName":"David", "CustomerAge":39}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c989081330fd0aa0d2fe4c4") }Following is ... Read More

MongoDB query to return only embedded document?

George John
Updated on 30-Jul-2019 22:30:25

331 Views

It is not possible to return only embedded document. However, it will return all the documents from a collection. Let us first implement the following query to create a collection with documents>db.queryToEmbeddedDocument.insertOne({"UserName":"Larry", "PostDetails":[{"UserMessage":"Hello", "UserLikes":8}, {"UserMessage":"Hi", "UserLikes":6}, {"UserMessage":"Good Morning", "UserLikes":12}, {"UserMessage":"Awesome", "UserLikes":4}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c988a9f330fd0aa0d2fe4bd") }Following is the query to display all documents from a collection with the help of find() method> db.queryToEmbeddedDocument.find().pretty();This will produce the following output{    "_id" : ObjectId("5c988a9f330fd0aa0d2fe4bd"),    "UserName" : "Larry",    "PostDetails" : [       {          "UserMessage" : "Hello",         ... Read More

Search a string with special characters in a MongoDB document?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

2K+ Views

To search a string with special characters in MongoDB document, you can use \. Here, we have special character $ in our string.Let us first implement the following query to create a collection with documents>db.searchDocumentWithSpecialCharactersDemo.insertOne({"UserId":"Smith$John123", "UserFirstName":"John", "UserLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c987b98330fd0aa0d2fe4b1") } >db.searchDocumentWithSpecialCharactersDemo.insertOne({"UserId":"Taylor$Carol983", "UserFirstName":"Carol", "UserLastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c987bdb330fd0aa0d2fe4b2") } >db.searchDocumentWithSpecialCharactersDemo.insertOne({"UserId":"Doe$John999", "UserFirstName":"John", "UserLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c987bee330fd0aa0d2fe4b3") } >db.searchDocumentWithSpecialCharactersDemo.insertOne({"UserId":"Miller$David555", "UserFirstName":"David", "UserLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c987c01330fd0aa0d2fe4b4") }Following is the query to display all documents from a collection with the help of ... Read More

Can we use NOT and AND together in MongoDB?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

161 Views

Yes, we can use the NOT and AND together in MongoDB. The syntax is as followsNOT X AND NOT Y = NOT (X AND Y) Let us see the working of above syntax. If both X and Y will be true then last result will be false. If one of the operands gives result false then last result will be true.Following is the query to create a collection with documents> db.NotAndDemo.insertOne({"StudentName":"John", "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c98746a330fd0aa0d2fe4a8") } > db.NotAndDemo.insertOne({"StudentName":"John", "StudentCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c987478330fd0aa0d2fe4a9") } > db.NotAndDemo.insertOne({"StudentName":"David", "StudentCountryName":"AUS"}); {   ... Read More

Advertisements