Found 1359 Articles for MongoDB

Find duplicate records in MongoDB?

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

1K+ Views

You can use the aggregate framework to find duplicate records in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330293b406bd3df60e01") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330493b406bd3df60e02") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330c93b406bd3df60e03") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a331093b406bd3df60e04") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a331593b406bd3df60e05") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Mike"}); {   ... Read More

How can I use 'Not Like' operator in MongoDB?

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

761 Views

For this, use the $not operator in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.notLikeOperatorDemo.insertOne({"StudentName":"John Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a29c393b406bd3df60dfc") } > db.notLikeOperatorDemo.insertOne({"StudentName":"John Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a29cc93b406bd3df60dfd") } > db.notLikeOperatorDemo.insertOne({"StudentName":"John Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a29df93b406bd3df60dfe") } > db.notLikeOperatorDemo.insertOne({"StudentName":"Carol Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a2a1693b406bd3df60dff") } > db.notLikeOperatorDemo.insertOne({"StudentName":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a2a2693b406bd3df60e00") }Display all documents from ... Read More

Which characters are NOT allowed in MongoDB field names?

Smita Kapse
Updated on 29-Jun-2020 16:00:38

584 Views

Do not use $ symbol or period (.) because these characters are not allowed for MongoDB field names. The field shouldn’t start with $.Here is an example of the allowed characters −> db.charactersAllowedDemo.insertOne({"Employee Name" : "John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7fefbc8d10a061296a3c6d") }Display all documents from a collection with the help of find() method. The query is as follows −> db.charactersAllowedDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c7fefbc8d10a061296a3c6d"),    "Employee Name" : "John" }

Difference between count() and find().count() in MongoDB?

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

272 Views

There is no difference between count() and find().count(). Let us see how both of them works. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.countDemo.insertOne({"UserId":1, "UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d278d10a061296a3c5d") } > db.countDemo.insertOne({"UserId":2, "UserName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d308d10a061296a3c5e") } > db.countDemo.insertOne({"UserId":3, "UserName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d3a8d10a061296a3c5f") } > db.countDemo.insertOne({"UserId":4, "UserName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d428d10a061296a3c60") }Display all documents from a collection with the help ... Read More

Querying an array of arrays in MongoDB?

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

710 Views

Use $in operator to query an array of arrays in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.arrayOfArraysDemo.insertOne({"EmployeeName":"Larry", "EmployeeSkills":[["Java", "MongoDB", "MySQL", "SQL Server"]]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f7a8d8d10a061296a3c5b") } > db.arrayOfArraysDemo.insertOne({"EmployeeName":"Mike", "EmployeeSkills":[["C", "C++"]]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f7aa68d10a061296a3c5c") }Display all documents from a collection with the help of find() method. The query is as follows −> db.arrayOfArraysDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c7f7a8d8d10a061296a3c5b"),    "EmployeeName" : "Larry",    "EmployeeSkills" : ... Read More

Matching an array field that contains any combination of the provided array in MongoDB?

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

177 Views

Use $nin operator along with $elemMatch and $not for this. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.combinationOfArrayDemo.insertOne({"StudentName":"Larry", "StudentAge":21, "StudentFavouriteTechnicalSubject":["C", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f77cc8d10a061296a3c58") } > db.combinationOfArrayDemo.insertOne({"StudentName":"Mike", "StudentAge":23, "StudentFavouriteTechnicalSubject":["C++", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f77dc8d10a061296a3c59") } > db.combinationOfArrayDemo.insertOne({"StudentName":"David", "StudentAge":22, "StudentFavouriteTechnicalSubject":["Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f77f48d10a061296a3c5a") }Display all documents from a collection with the help of find() method. The query is as follows −> db.combinationOfArrayDemo.find().pretty();The following is the output ... Read More

How to efficiently perform “distinct” with multiple keys in MongoDB?

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

287 Views

You can perform distinct with multiple keys with the help of an aggregate framework.To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Mike", "StudentAge":22, "StudentMathMarks":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f74488d10a061296a3c53") } > db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Mike", "StudentAge":22, "StudentMathMarks":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f744b8d10a061296a3c54") } > db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Bob", "StudentAge":23, "StudentMathMarks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f74598d10a061296a3c55") } > db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Bob", "StudentAge":23, "StudentMathMarks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f745e8d10a061296a3c56") } > db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Carol", "StudentAge":27, "StudentMathMarks":54}); {    "acknowledged" ... Read More

How do I make case-insensitive queries on MongoDB?

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

153 Views

Use regexp to make case-insensitive queries on MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.caseInsensitiveDemo.insertOne({"UserName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f6fec8d10a061296a3c45") } > db.caseInsensitiveDemo.insertOne({"UserName":"DAVID"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f6ff28d10a061296a3c46") } > db.caseInsensitiveDemo.insertOne({"UserName":"david"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f6ffa8d10a061296a3c47") } > db.caseInsensitiveDemo.insertOne({"UserName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f70008d10a061296a3c48") } > db.caseInsensitiveDemo.insertOne({"UserName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f70058d10a061296a3c49") } > db.caseInsensitiveDemo.insertOne({"UserName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ... Read More

How to remove an element from a doubly-nested array in a MongoDB document?

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

2K+ Views

To remove an element from a doubly-nested array in MongoDB document, you can use $pull operator.To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.removeElementFromDoublyNestedArrayDemo.insertOne(    ... {       ... "_id" : "1",       ... "UserName" : "Larry",       ... "UserDetails" : [          ... {             ... "UserCountryName" : "US",             ... "UserLocation" : [                ... { ... Read More

Include all existing fields and add new fields to document in MongoDB?

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

314 Views

You can achieve this with the help of $addFields operator. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.addFieldDemo.insertOne({"EmployeeId":101, "EmployeeName":"Larry", "EmployeeDetails":{    "EmployeeSalary":65000, "EmployeeCity":"New York", "Message":"Hi"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f654d8d10a061296a3c44") }Display all documents from a collection with the help of find() method. The query is as follows −> db.addFieldDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c7f654d8d10a061296a3c44"),    "EmployeeId" : 101,    "EmployeeName" : "Larry",    "EmployeeDetails" : {       "EmployeeSalary" : 65000,     ... Read More

Advertisements