Found 1661 Articles for Big Data Analytics

Find documents with arrays not containing a document with a particular field value in MongoDB?

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

899 Views

You can use $nin 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.documentWithAParticularFieldValueDemo.insertOne(    ... {       ...       ... "StudentId" : 101,       ... "StudentDetails" :       ... [          ... {             ... "TheoryMarks": 78,             ... "PracticalMarks": 91          ... },          ... {             ... Read More

How to return only unique values (no duplicates) in MongoDB?

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

1K+ Views

You can use distinct() to return only unique values. The syntax is as follows −db.yourCollectionName.distinct("yourFieldName");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.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Larry", "CustomerAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed7262f684a30fbdfd580") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Mike", "CustomerAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed72d2f684a30fbdfd581") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Sam", "CustomerAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed7322f684a30fbdfd582") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Carol", "CustomerAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed73a2f684a30fbdfd583") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"David", "CustomerAge":22}); {    "acknowledged" : true,    "insertedId" ... Read More

Find largest document size in MongoDB?

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

840 Views

To find the largest document size in MongoDB, you need to write a script in the shell.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.largestDocumentDemo.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed2e32f684a30fbdfd57d") } > db.largestDocumentDemo.insertOne({"StudentName":"Carol", "StudentAge":22, "StudentCountryName":"US", "TechnicalSubject":["C", "C++", "Java", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed3282f684a30fbdfd57e") } > db.largestDocumentDemo.insertOne({"StudentName":"Mike", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed3382f684a30fbdfd57f") }Display all documents from a collection with the help of find() method. The query is as follows −> ... Read More

How to find through list of ids in MongoDB?

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

2K+ Views

You can use $in operator to find through the list of ids 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.findListOfIdsDemo.insertOne({"StudentName":"Carol", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ecadd2f684a30fbdfd575") } > db.findListOfIdsDemo.insertOne({"StudentName":"Bob", "StudentAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ecae42f684a30fbdfd576") } > db.findListOfIdsDemo.insertOne({"StudentName":"David", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ecaed2f684a30fbdfd577") } > db.findListOfIdsDemo.insertOne({"StudentName":"John", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ecaf82f684a30fbdfd578") } > db.findListOfIdsDemo.insertOne({"StudentName":"Mike", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" ... Read More

MongoDB query with an 'or' condition?

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

469 Views

To understand the query with an or condition, let us create a collection with the document. The query to create a collection with a document is as follows −> db.orConditionDemo.insertOne({"CustomerName":"Larry", "ShippingDate":new ISODate("2018-01-29")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ec5262f684a30fbdfd56a") } > db.orConditionDemo.insertOne({"CustomerName":"Mike", "ShippingDate":new ISODate("2019-04-13")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ec5362f684a30fbdfd56b") } > db.orConditionDemo.insertOne({"CustomerName":"Bob", "ShippingDate":new ISODate("2019-02-21")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ec5422f684a30fbdfd56c") } > db.orConditionDemo.insertOne({"CustomerName":"David", "ShippingDate":new ISODate("2019-03-15")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ec5532f684a30fbdfd56d") } > db.orConditionDemo.insertOne({"CustomerName":"John", "ShippingDate":new ISODate("2019-03-19")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ec56c2f684a30fbdfd56e") }Display all documents ... Read More

The collection.find() always returns all fields with MongoDB?

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

580 Views

You can return a specific field from collection.find() by using the following syntax.Case 1 − The syntax is as follows −db.yourCollectionName.find({}, {"yourFieldName":1}).pretty();The above field name is set to 1 means it will return only that field. If you set to 0 it will return all fields except the field which is set to 0.Case 2 − The syntax is as follows −db.yourCollectionName.find({}, {"yourFieldName":0}).pretty();To understand the above syntaxes, let us create a collection with document. The query to create a collection with document is as follows −> db.returnFieldInFindDemo.insertOne({"StudentName":"John", "StudentAge":23, "TechnicalSubject":["MongoDB", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ebfe72f684a30fbdfd566") } ... Read More

MongoDB 'count()' is very slow. How do we work around with it?

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

1K+ Views

You can use ensureIndex() to boost the performance of count() method 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.countPerformanceDemo.insertOne({"StudentName":"John", "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ebcf82f684a30fbdfd55f") } > db.countPerformanceDemo.insertOne({"StudentName":"Mike", "StudentCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ebd042f684a30fbdfd560") } > db.countPerformanceDemo.insertOne({"StudentName":"David", "StudentCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ebd112f684a30fbdfd561") } > db.countPerformanceDemo.insertOne({"StudentName":"Carol", "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ebd1a2f684a30fbdfd562") } > db.countPerformanceDemo.insertOne({"StudentName":"Bob", "StudentCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ... Read More

Resolve ‘multi update only works with $ operators’ in MongoDb?

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

836 Views

You can use $set operator for this. The syntax is as follows −db.yourCollectionName.update({ }, {'$set': "yourFieldName": "yourValue" }, false, 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.unconditionalUpdatesDemo.insertOne({"ClientName":"Larry", "ClientAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8eb7372f684a30fbdfd557") } > db.unconditionalUpdatesDemo.insertOne({"ClientName":"Mike", "ClientAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8eb73f2f684a30fbdfd558") } > db.unconditionalUpdatesDemo.insertOne({"ClientName":"Sam", "ClientAge":27}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8eb7462f684a30fbdfd559") } > db.unconditionalUpdatesDemo.insertOne({"ClientName":"Carol", "ClientAge":29}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8eb7502f684a30fbdfd55a") }Display all documents from a ... Read More

Case insensitive search in Mongo?

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

3K+ Views

You can restrict case insensitive search in MongoDB with the help of '$regex'. The syntax is as follows −db.yourCollectionName.find({"yourFieldName" : { '$regex':'^yourValue$'}});You can use another regex. The syntax is as follows −db.yourCollectionName.find({"Name" : { '$regex':/^yourValue$/i}});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.caseInsesitiveDemo.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bd66293c80e3f23815e83") } > db.caseInsesitiveDemo.insertOne({"Name":"Johnson"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bd66693c80e3f23815e84") } > db.caseInsesitiveDemo.insertOne({"Name":"Johny"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bd66a93c80e3f23815e85") }Display all documents from a collection with ... Read More

Is it possible to write to MongoDB console in JavaScript execution?

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

117 Views

To write on console, you need to use print() method. The syntax is as follows −print(“yourString”);To display objects, you can use printjson(). The syntax is as follows −printjson(yourObjectName);Let us implement both the functions. The first query is as follows to display something −> print("Welcome to MongoDB Console");The following is the output on a console −Welcome to MongoDB ConsoleLet us create an object. The query is as follows −>studentInformation={"StudentName":"John", "StudentAge":24, "StudentTechnicalSkills":["C", "C++", "Java", "MongoDB", "MySQL"]}; {    "StudentName" : "John",    "StudentAge" : 24,    "StudentTechnicalSkills" : [       "C",       "C++",       "Java",     ... Read More

Advertisements