Found 6702 Articles for Database

Count distinct value in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

309 Views

Use the concept of length to count distinct value. Following is the syntax −db.yourCollectionName.distinct("yourFieldName").length;Let us create a collection with documents −> db.countDistinctDemo.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6166de8cc557214c0dfa") } > db.countDistinctDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd616ade8cc557214c0dfb") } > db.countDistinctDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd616cde8cc557214c0dfc") } > db.countDistinctDemo.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6170de8cc557214c0dfd") } > db.countDistinctDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6175de8cc557214c0dfe") } > db.countDistinctDemo.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6181de8cc557214c0dff") }Display all documents from a collection with the help ... Read More

Get the count of the number of documents in a MongoDB Collection?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

160 Views

To get the count of the number of documents in a collection MongoDB, you can use the below syntax −db.getCollectionNames().map(function(anyVariableName) {    return { "yourVariableName": yourVariableName, "count": db[yourVariableName].count() } });Here, we are using ‘test’ database.Let us implement the above syntax to get the count of the number of documents in a MongoDB collection −> db.getCollectionNames().map(function(ColName) { ... return { "ColName": ColName, "TotalDocument": db[ColName].count() } ... });This will produce the following output −[    {       "ColName" : "ConvertStringToDateDemo",       "TotalDocument" : 4    },    {       "ColName" : "Employee_Information",       "TotalDocument" ... Read More

Get index of given element in array field in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

908 Views

You can use $indexOfArray operator for this. Let us create a collection with documents −>db.getIndexDemo.insertOne({"InstructorName":"Chris", "InstructorSubject":["MongoDB", "MySQL", "Java", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd5251de8cc557214c0df8") }Display all documents from a collection with the help of find() method −> db.getIndexDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbd5251de8cc557214c0df8"),    "InstructorName" : "Chris",    "InstructorSubject" : [       "MongoDB",       "MySQL",       "Java",       "C++"    ] }Following is the query to get index of given element in an array field in MongoDB −> db.getIndexDemo.aggregate( [ { "$project": { ... Read More

How to query MongoDB using the $ne operator?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

93 Views

To query MongoDB using the $ne operator, following is the syntax −db.yourCollectionName.find({yourFieldName:{$ne:yourValue}}).pretty();Let us create a collection with documents −> db.notEqaulToDemo.insertOne({"StudentName":"Larry", "StudentMathMarks":68}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a6bde8cc557214c0ded") } > db.notEqaulToDemo.insertOne({"StudentName":"Chris", "StudentMathMarks":88}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a79de8cc557214c0dee") } > db.notEqaulToDemo.insertOne({"StudentName":"David", "StudentMathMarks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a89de8cc557214c0def") } > db.notEqaulToDemo.insertOne({"StudentName":"Carol", "StudentMathMarks":69}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a97de8cc557214c0df0") }Display all documents from a collection with the help of find() method −> db.notEqaulToDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbd3a6bde8cc557214c0ded"),    "StudentName" : "Larry",    "StudentMathMarks" : ... Read More

Unset an attribute from a single array element in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

107 Views

Use $unset operator to unset an attribute. Let us first create a collection with documents −> db.unsetAnAttributeDemo.insertOne( ...    { ...       _id: 1, ...       "StudentDetails": [ ...          { ...             "StudentFirstName": "Ramit", ...             "StudentCountryName":"UK" ...          }, ...          { ...             "StudentFirstName": "Bob", ...             "StudentCountryName":"US" ...          }, ...          { ...       ... Read More

Search by property name for any document with that property in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

181 Views

You can use $ne operator for this. Let us first create a collection with documents −> db.searchByPropertyName.insertOne({"FirstName":"Larry", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbaf7af7219729fde21ddb5") } > db.searchByPropertyName.insertOne({"FirstName":null, "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbaf7b97219729fde21ddb6") } > db.searchByPropertyName.insertOne({"FirstName":"John", "Age":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbaf7c57219729fde21ddb7") } > db.searchByPropertyName.insertOne({"FirstName":null, "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbaf7d07219729fde21ddb8") } > db.searchByPropertyName.insertOne({"FirstName":"David", "Age":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbaf7df7219729fde21ddb9") }Following is the query to display all documents from the collection with the help of find() prettyprint −> db.searchByPropertyName.find().pretty();This will produce ... Read More

MongoDB query to find the highest numeric value of a column?

Samual Sam
Updated on 02-Jul-2020 11:33:59

135 Views

You can use $not operator along with $type for this. Let us first create a collection with documents −> db.highestNumericValueOfAColumnDemo.insertOne( ...    { ...       "StudentName": "John", ...       "StudentMathMarks":69 ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cba05727219729fde21ddb1") } > db.highestNumericValueOfAColumnDemo.insertOne( ...    { ...       "StudentName": "Carol", ...       "StudentMathMarks":"89" ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cba059d7219729fde21ddb2") } > db.highestNumericValueOfAColumnDemo.insertOne( ...    { ...       "StudentName": "Chris", ...       "StudentMathMarks":82 ...    } ... ... Read More

Delete a field and value in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

387 Views

To delete a MongoDB field and value, you can use $unset operator. Let us first create a collection with documents −> db.deleteFieldDemo.insertOne({"FirstName":"John", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9fb767219729fde21ddad") } > db.deleteFieldDemo.insertOne({"FirstName":"David", "LastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9fb837219729fde21ddae") } > db.deleteFieldDemo.insertOne({"FirstName":"Carol", "LastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9fb8d7219729fde21ddaf") }Following is the query to display all documents from the collection with the help of find() method −> db.deleteFieldDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cb9fb767219729fde21ddad"),    "FirstName" : "John",    "LastName" : "Smith" } {    "_id" : ... Read More

How to read a specific key-value pair from a MongoDB collection?

Samual Sam
Updated on 30-Jul-2019 22:30:25

773 Views

You can use dot(.) notation to read a specific key-value pair from MongoDB collection. Let us first create a collection with documents −> db.readSpecificKeyValueDemo.insertOne( ...    { ...       "_id": 100, ...       "StudentDetails": ...       { ...          "StudentFirstName" : "David", ...          "StudentLastName" :"Miller", ...          "StudentAge":23, ...          "StudentCountryName":"US" ...       } ...    } ... ); { "acknowledged" : true, "insertedId" : 100 }Following is the query to display all documents from the collection with the ... Read More

How to remove duplicate values inside a list in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

902 Views

You can use aggregate framework along with $setUnion operator. Let us first create a collection with documents −> db.removeDuplicatesDemo.insertOne({"InstructorName":"Chris", "InstructorAge":34, "InstructorSubject":    ["Java", "C", "Java", "C++", "MongoDB", "MySQL", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9d96c895c4fd159f80807") }Following is the query to display all documents from the collection with the help of find() method −> db.removeDuplicatesDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cb9d96c895c4fd159f80807"),    "InstructorName" : "Chris",    "InstructorAge" : 34,    "InstructorSubject" : [       "Java",       "C",       "Java",       "C++",       "MongoDB",   ... Read More

Advertisements