Found 1349 Articles for MongoDB

Perform aggregation sort in MongoDB?

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

190 Views

You can use aggregate() method along with $sort() operator 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.aggregationSortDemo.insertOne({"StudentId":98, "StudentFirstName":"John", "StudentLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90140c5705caea966c5587") } > db.aggregationSortDemo.insertOne({"StudentId":128, "StudentFirstName":"Carol", "StudentLastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90141b5705caea966c5588") } > db.aggregationSortDemo.insertOne({"StudentId":110, "StudentFirstName":"David", "StudentLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90142f5705caea966c5589") } > db.aggregationSortDemo.insertOne({"StudentId":139, "StudentFirstName":"Chris", "StudentLastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90146a5705caea966c558a") } > db.aggregationSortDemo.insertOne({"StudentId":125, "StudentFirstName":"Sam", "StudentLastName":"Williams"}); {    "acknowledged" : true, ... Read More

Select MongoDB documents where a field either does not exist, is null, or is false?

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

333 Views

You can use $in operator for this. Let us first create a collection with a document. The query to create a collection with a document is as follows −> db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":1, "StudentName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9010215705caea966c557f") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":2, "StudentName":"Mike", "hasAgeGreaterThanOrEqualTo18":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90106a5705caea966c5580") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":3, "StudentName":"Carol", "hasAgeGreaterThanOrEqualTo18":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9010795705caea966c5581") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":4, "StudentName":"Sam", "hasAgeGreaterThanOrEqualTo18":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9010865705caea966c5582") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":5, "StudentName":"David", "hasAgeGreaterThanOrEqualTo18":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9010945705caea966c5583") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":6, "StudentName":"Chris", ... Read More

MongoDB print JSON without whitespace i.e. unpretty JSON?

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

568 Views

To print unpretty json, use the following syntax −var yourVariableName= db.yourCollectionName.find().sort({_id:-1}).limit(10000); while( yourVariableName.hasNext() ) {    printjsononeline(yourVariableName.next() ); };To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.unprettyJsonDemo.insertOne({"StudentName":"John", "StudentAge":21, "StudentTechnicalSkills":["C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900df25705caea966c557d") } > db.unprettyJsonDemo.insertOne({"StudentName":"Carol", "StudentAge":22, "StudentTechnicalSkills":["MongoDB", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900e085705caea966c557e") }all documents from a collection with the help of find() method. The query is as follows −> db.unprettyJsonDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c900df25705caea966c557d"),   ... Read More

Getting the highest value of a column in MongoDB?

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

845 Views

To get the highest value of a column in MongoDB, you can use sort() along with limit(1). The syntax is as follows −db.yourCollectionName.find().sort({"yourFieldName":-1}).limit(1);To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.gettingHighestValueDemo.insertOne({"Value":1029}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900b885705caea966c5574") } > db.gettingHighestValueDemo.insertOne({"Value":3029}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900b8d5705caea966c5575") } > db.gettingHighestValueDemo.insertOne({"Value":1092}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900b925705caea966c5576") } > db.gettingHighestValueDemo.insertOne({"Value":18484}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900b955705caea966c5577") } > db.gettingHighestValueDemo.insertOne({"Value":37474}); {    "acknowledged" ... Read More

MongoDB query with fields in the same document?

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

119 Views

You can use $where operator 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.queryInSameDocumentsDemo.insertOne({"StudentDetails":{"StudentName":"John"}, "NewStudentDetails":{"StudentName":"Carol"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90096ed3c9d04998abf017") } > db.queryInSameDocumentsDemo.insertOne({"StudentDetails":{"StudentName":"Bob"}, "NewStudentDetails":{"StudentName":"Bob"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900a435705caea966c5573") }Display all documents from a collection with the help of find() method. The query is as follows −> db.queryInSameDocumentsDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c90096ed3c9d04998abf017"),    "StudentDetails" : {       "StudentName" : "John"    },    "NewStudentDetails" : { ... Read More

In MongoDB how do you use $set to update a nested value/embedded document?

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

1K+ Views

The syntax is as follows for this −db.yourCollectionName.update({ }, { $set: { "yourOuterFieldName.yourInnerFieldName": "yourValue" } });To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.updateNestedValueDemo.insertOne({"CustomerName":"Chris",    ... "CustomerDetails":{"CustomerAge":25, "CustomerCompanyName":"Google", "CustomerCityName":"US"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fccc4d3c9d04998abf015") }Display all documents from a collection with the help of find() method. The query is as follows −> db.updateNestedValueDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c8fccc4d3c9d04998abf015"),    "CustomerName" : "Chris",    "CustomerDetails" : {       "CustomerAge" : 25,       ... Read More

List all values of a certain field in MongoDB?

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

1K+ Views

To get the list of all values of certain fields in MongoDB, you can use distinct(). The syntax is as follows −db.yourCollectionName.distinct( "yourFieldName");To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[10, 20, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fc89ed3c9d04998abf011") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[40, 50, 60]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fc8abd3c9d04998abf012") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[10, 20, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fc8d7d3c9d04998abf013") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[40, 50, 70]}); {    "acknowledged" : true,    "insertedId" ... Read More

How to create a nested index in MongoDB?

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

366 Views

To create nested index in MongoDB, you can use createIndex() or ensureIndex(). The syntax is as follows −db.yourCollectionName.createIndex({"yourOuterFieldName.yourInnerFieldName.yourSecondInnerFieldName": 1});To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.nestedIndexDemo.insertOne(    ... {       ...       ... "CustomerId":101,       ... "CustomerDetails":       ... {          ... "CustomerListDetails":          ... {             ... "CustomerName":"Larry",             ... "CustomerProjectName": "Project-1",           ... Read More

Delete all elements in an array field in MongoDB?

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

1K+ Views

You can use $set operator for this. The syntax is as follows −db.yourCollectionName.update({}, { $set : {"yourFieldName": [] }} , {multi:true} );To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.deleteAllElementsInArrayDemo.insertOne({"InstructorName":"Larry", "InstructorTechnicalSubject":["Java", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fb971d3c9d04998abf00e") } > db.deleteAllElementsInArrayDemo.insertOne({"InstructorName":"Mike", "InstructorTechnicalSubject":["C", "C++", "Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fb98ad3c9d04998abf00f") }Display all documents from a collection with the help of find() method. The query is as follows −> db.deleteAllElementsInArrayDemo.find().pretty();The following is the output −{    "_id" ... Read More

How to sort in MongoDB?

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

124 Views

To sort in MongoDB, you can use the sort() method.Case 1 − Sort in ascending order. The syntax is as follows −db.yourCollectionName.find().sort({yourField:1});Case 2 − Sort in descending order. The syntax is as follows −db.yourCollectionName.find().sort({yourField:-1});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.sortingDemo.insertOne({"Value":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f8e2ed3c9d04998abf006") } > db.sortingDemo.insertOne({"Value":1}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f8e31d3c9d04998abf007") } > db.sortingDemo.insertOne({"Value":150}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f8e34d3c9d04998abf008") } > db.sortingDemo.insertOne({"Value":250}); {    "acknowledged" : true, ... Read More

Advertisements