Found 1661 Articles for Big Data Analytics

Avoid duplicate entries in MongoDB?

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

1K+ Views

To avoid duplicate entries in MongoDB, you can use createIndex(). The syntax is as follows −db.yourCollectionName.createIndex({"yourFieldName":1}, {unique:true});Let us implement the above syntax. The query to avoid duplicate entries in MongoDB is a follows −> db.avoidDuplicateEntriesDemo.createIndex({"UserName":1}, {unique:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Now insert some records in the above collection. The query to insert record is as follows −> db.avoidDuplicateEntriesDemo.insertOne({"UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90e1824afe5c1d2279d697") }Here is the error whenever you try to insert the same record once again −> db.avoidDuplicateEntriesDemo.insertOne({"UserName":"John"}); 2019-03-19T18:03:08.465+0530 E QUERY [js] ... Read More

Querying array elements with MongoDB?

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

295 Views

MongoDB is better when you are querying array elements. Let us use the following syntax for querying array elements −db.yourCollectionName.find({yourArrayFieldName:"yourValue"}).pretty();The above syntax will return all those documents which have the value “yourValue” in an array field.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.queryArrayElementsDemo.insertOne({    ... "StudentName":"John", "StudentFavouriteSubject":["C", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90c0354afe5c1d2279d694") } > db.queryArrayElementsDemo.insertOne({ "StudentName":"Carol", "StudentFavouriteSubject":["C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90c0434afe5c1d2279d695") } > db.queryArrayElementsDemo.insertOne({ "StudentName":"David", "StudentFavouriteSubject":["MongoDB", "Java"]}); {    "acknowledged" : ... Read More

How to know which storage engine is used in MongoDB?

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

302 Views

To know which storage engine is used in MongoDB, you can use storageEngine. The syntax is as follows −db.serverStatus().storageEngine;To know all the configuration details of storage engine, you can use the following syntax:db.serverStatus().yourStorageEngineName;Let us implement the above syntax to know which storage engine is being used in MongoDB. The query is as follows −> db.serverStatus().storageEngine;The following is the output −{    "name" : "wiredTiger",    "supportsCommittedReads" : true,    "supportsSnapshotReadConcern" : true,    "readOnly" : false,    "persistent" : true }In order to know all configuration details about the above storage engine, the query is as follows −> db.serverStatus().wiredTiger;The following ... Read More

How to get distinct list of sub-document field values in MongoDB?

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

341 Views

To get distinct list of sub-document field values, you can use dot(.). The syntax is as follows −db.yourCollectionName.distinct("yourOuterFieldName.yourInnerFieldName");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.getDistinctListOfSubDocumentFieldDemo.insertOne(    ... {       ... "StudentId": 101,       ... "StudentPersonalDetails": [          ... {             ... "StudentName": "John",             ... "StudentAge":24          ... },          ... {             ... ... Read More

Perform aggregation sort in MongoDB?

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

266 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

403 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

682 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

1K+ 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

198 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

Advertisements