Found 1661 Articles for Big Data Analytics

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

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

673 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

421 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

239 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

How to remove document on the basis of _id in MongoDB?

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

230 Views

To remove document on the basis of _id in MongoDB, implement the following syntaxdb.yourCollectionName.remove({“_id”:ObjectId(“yourId”});Let us first implement the following query to create a collection with documents>db.removeDocumentOnBasisOfId.insertOne({"UserName":"Larry", "UserAge":23, "UserCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986f9f330fd0aa0d2fe4a3") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Sam", "UserAge":21, "UserCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fb4330fd0aa0d2fe4a4") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Chris", "UserAge":24, "UserCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fc0330fd0aa0d2fe4a5") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Robert", "UserAge":26, "UserCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fcf330fd0aa0d2fe4a6") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"David", "UserAge":28, "UserCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fed330fd0aa0d2fe4a7") }Following is the query to display all documents from a collection ... Read More

Increment a value in a MongoDB nested object?

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

489 Views

To increment a value in nested object, you can use $inc operator. Let us first implement the following query to create a collection with documents>db.incrementValueDemo.insertOne({"StudentName":"Larry", "StudentCountryName":"US", "StudentDetails":[{"StudentSubjectName":"Math", "StudentMathMarks":79}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986ca0330fd0aa0d2fe4a2") }Following is the query to display all documents from a collection with the help of find() method> db.incrementValueDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c986ca0330fd0aa0d2fe4a2"),    "StudentName" : "Larry",    "StudentCountryName" : "US",    "StudentDetails" : [       {          "StudentSubjectName" : "Math",          "StudentMathMarks" : 79       }    ] ... Read More

Get the duplicate values of a field in MongoDB?

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

1K+ Views

Use aggregate() method to get the duplicate value of a field. Let us first create a collection with documents using the following query> db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995078863d6ffd454bb647") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995081863d6ffd454bb648") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995089863d6ffd454bb649") } > db.findAllNonDistinctDemo.insertOne({"UserName":"David", "UserAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995093863d6ffd454bb64a") } > db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c99509d863d6ffd454bb64b") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert", "UserAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9950a7863d6ffd454bb64c") } ... Read More

Inserting the current datetime in MongoDB?

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

3K+ Views

To insert current datetime in MongoDB, use the $setOnInsert operator. Let us first implement the following query to create a collection with documents>db.addCurrentDateTimeDemo.insertOne({"StudentName":"John", "StudentAdmissionDate":new Date("2012-01-21") }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97ae45330fd0aa0d2fe49f") } >db.addCurrentDateTimeDemo.insertOne({"StudentName":"Carol", "StudentAdmissionDate":new Date("2013-05-24") }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97ae54330fd0aa0d2fe4a0") } >db.addCurrentDateTimeDemo.insertOne({"StudentName":"Carol", "StudentAdmissionDate":new Date("2019-07-26") }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97ae5f330fd0aa0d2fe4a1") }Following is the query to display all documents from a collection with the help of find() method> db.addCurrentDateTimeDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c97ae45330fd0aa0d2fe49f"),    "StudentName" : "John",    "StudentAdmissionDate" : ISODate("2012-01-21T00:00:00Z") } { ... Read More

How to search document in MongoDB by _id

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

415 Views

To search a document in MongoDB by _id, you need to call ObjectId(). Let us first see the syntaxdb.yourCollectionName.find({"_id":ObjectId("yourId")}).pretty();To understand the concept and search a document, let us implement the following query to create a collection with documents> db.searchDocumentDemo.insertOne({"UserId":1, "UserName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8e4330fd0aa0d2fe487") } > db.searchDocumentDemo.insertOne({"UserId":2, "UserName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8ea330fd0aa0d2fe488") } > db.searchDocumentDemo.insertOne({"UserId":3, "UserName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8f1330fd0aa0d2fe489") } > db.searchDocumentDemo.insertOne({"UserId":4, "UserName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8fa330fd0aa0d2fe48a") } > db.searchDocumentDemo.insertOne({"UserId":5, "UserName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ... Read More

How to create double nested array in MongoDB?

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

333 Views

To create a double nested array in MongoDB, let us implement the query to create a collection with documents. Within that, we have created a double nested array that displays Student Details, with the project name and technologies used to develop the same project:> db.doubleNestedArrayDemo.insertOne( ... { ...    "StudentId" : "1000", ...    "StudentName" : "Larry", ...    "StudentDetails" : [ ...    { ...       "ProjectName" : "Online Banking", ...       "ProjectDetails" : [ ...       { ...          "TechnologyUsed" : "Java" ...       }, ...   ... Read More

Advertisements