Found 1659 Articles for Big Data Analytics

How to match multiple criteria inside an array with MongoDB?

AmitDiwan
Updated on 30-Mar-2020 08:36:36

842 Views

To match multiple criteria inside an array, use aggregate(). Let us create a collection with documents −> db.demo84.insertOne({ ...    "EmployeeDetails": [ ...       {Name: 'John', Salary:45000, isMarried: true}, ...       {Name: 'Chris', Salary:50000, isMarried: false} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2c0a3471bf0181ecc422a5") } > db.demo84.insertOne({ ...    "EmployeeDetails": [ ...       {Name: 'Sam', Salary:56000, isMarried: false}, ...       {Name: 'Bob', Salary:50000, isMarried: false} ...       ] ...    } ... ); {    "acknowledged" : true,   ... Read More

MongoDB “$and” operator for subcollection to fetch a document?

AmitDiwan
Updated on 30-Mar-2020 08:33:17

381 Views

To fetch a document, use $in, instead of $and in MongoDB. Let us first create a collection with documents −> db.demo83.insertOne( ... { ...    "Details":[ ...       { ...          "Name":"Chris", ...          "Subject":[ ...             "MySQL", ...             "MongoDB" ...          ] ...       }, ...       { ...          "Name":"David", ...          "Subject":[ ...          "Java", ...          "C" ... ... Read More

How to make a unique field in MongoDB?

AmitDiwan
Updated on 30-Mar-2020 08:28:03

960 Views

To make a unique field in MongoDB, use unique − true. Let us create a collection with documents −> db.demo82.createIndex({"EmployeeName":1}, {unique:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo82.insertOne({"EmployeeName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bfb1b71bf0181ecc422a0") } > db.demo82.insertOne({"EmployeeName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bfb1f71bf0181ecc422a1") } > db.demo82.insertOne({"EmployeeName":"Chris"}); 2020-01-25T13:54:00.410+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo82 index: EmployeeName_1 dup key: { : "Chris" } : WriteError({    "index" : 0,    "code" : 11000,    "errmsg" : "E11000 duplicate key ... Read More

Finding a specific item in subdocument with MongoDB?

AmitDiwan
Updated on 30-Mar-2020 08:25:06

106 Views

To get a specific item in a sudocument, use the dot(.) notation. Let us create a collection with documents −> db.demo81.insertOne({"StudentDetails":[{"StudentName":"Carol", "StudentSubject":"Java"}, { "StudentName" : "David", "StudentSubject" : "MongoDB" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bf6ec71bf0181ecc4229d") } > db.demo81.insertOne({"StudentDetails":[{"StudentName":"Mike", "StudentSubject":"Python"}, { "StudentName" : "David", "StudentSubject" : "C" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bf70471bf0181ecc4229e") } > db.demo81.insertOne({"StudentDetails":[{"StudentName":"Jace", "StudentSubject":"C++"}, {    "StudentName" : "John", "StudentSubject" : "MySQL" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bf72071bf0181ecc4229f") }Display all documents from a collection with the help of find() method −> db.demo81.find();This will produce the following ... Read More

MongoDB query to calculate average

AmitDiwan
Updated on 30-Mar-2020 08:21:23

377 Views

To calculate average in MongoDB, use $avg. Let us create a collection with documents −> db.demo80.insertOne({"Details":{"Price":10.5}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bf43271bf0181ecc42297") } > db.demo80.insertOne({"Details":{"Price":50.3}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bf43871bf0181ecc42298") } > db.demo80.insertOne({"Details":{"Price":100.10}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bf43f71bf0181ecc42299") }Display all documents from a collection with the help of find() method −> db.demo80.find();This will produce the following output −{ "_id" : ObjectId("5e2bf43271bf0181ecc42297"), "Details" : { "Price" : 10.5 } } { "_id" : ObjectId("5e2bf43871bf0181ecc42298"), "Details" : { "Price" : 50.3 } } { "_id" : ObjectId("5e2bf43f71bf0181ecc42299"), "Details" : { "Price" ... Read More

MongoDB query to implement OR operator in find()

AmitDiwan
Updated on 30-Mar-2020 08:15:43

75 Views

Let us create a collection with documents −> db.demo78.insertOne({"Name1":"Chris", "Name2":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bd99c71bf0181ecc4228f") } > db.demo78.insertOne({"Name1":"Bob", "Name2":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bd9ac71bf0181ecc42290") } > db.demo78.insertOne({"Name1":"David", "Name2":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bd9b671bf0181ecc42291") } > db.demo78.insertOne({"Name1":"Jace", "Name2":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bd9bf71bf0181ecc42292") }Display all documents from a collection with the help of find() method −> db.demo78.find();This will produce the following output −{ "_id" : ObjectId("5e2bd99c71bf0181ecc4228f"), "Name1" : "Chris", "Name2" : "Mike" } { "_id" : ObjectId("5e2bd9ac71bf0181ecc42290"), "Name1" : "Bob", "Name2" : "Carol" } { ... Read More

How to update MongoDB Object?

AmitDiwan
Updated on 30-Mar-2020 08:12:20

263 Views

To update MongoDB object, use UPDATE(). Let us create a collection with documents −> db.demo77.insertOne({"Details" : { "Score" : 78 } }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bd6f371bf0181ecc4228a") }Display all documents from a collection with the help of find() method −> db.demo77.find();This will produce the following output −{ "_id" : ObjectId("5e2bd6f371bf0181ecc4228a"), "Details" : { "Score" : 78 } }Following is the query to update MongoDB object −> db.demo77.update({'Details.Score':78}, {$set:{'Details.Score':89}}, {multi:true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with the help of find() method −> db.demo77.find();This will produce the ... Read More

Multilevel $group using MongoDB

AmitDiwan
Updated on 30-Mar-2020 08:07:44

294 Views

To implement multilevel $group, use MongoDB aggregate. Let us create a collection with documents −> db.demo76.insertOne({ Name:"Chris", "Age" : 21, "CountryName" : 'US' }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bd3e571bf0181ecc42281") } > db.demo76.insertOne({ Name:"Chris", "Age" : 21, "CountryName" : 'US' }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bd3e571bf0181ecc42282") } > db.demo76.insertOne({ Name:"Chris", "Age" : 23, "CountryName" : 'UK' }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bd3e571bf0181ecc42283") } > db.demo76.insertOne({ Name:"Chris", "Age" : 23, "CountryName" : 'UK' }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bd3e571bf0181ecc42284") } > db.demo76.insertOne({ Name:"Chris", "Age" : 21, ... Read More

MongoDB query to get only a specific number of elements

AmitDiwan
Updated on 30-Mar-2020 08:04:40

176 Views

To return only a specific number of elements, use aggregate() and $slice. Let us create a collection with documents −> db.demo75.insertOne({"Name":["Sam", "Mike", "Carol", "David", "Bob", "John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bcd7671bf0181ecc42278") }Display all documents from a collection with the help of find() method −> db.demo75.find();This will produce the following output −{ "_id" : ObjectId("5e2bcd7671bf0181ecc42278"), "Name" : [ "Sam", "Mike", "Carol", "David", "Bob", "John" ] }Following is the slice query in MongoDB −> db.demo75.aggregate([ { $project: { Name: { $slice: [ "$Name", 4 ] } } } ]);This will produce the following output −{ "_id" : ObjectId("5e2bcd7671bf0181ecc42278"), ... Read More

Find MongoDB documents where all objects in array have specific value?

AmitDiwan
Updated on 30-Mar-2020 08:01:30

447 Views

Let us create a collection with documents −> db.demo74.insertOne( ... { ... StudentName: "Chris", ... StudentDetails: [{ ...    "Subject": "MongoDB", ...    "isRegular": "Active" ...    }, { ...       "Subject": "MongoDB", ...       "isRegular": "InActive" ...    }, { ...       "Subject": "MongoDB", ...       "isRegular": "InActive" ...    }] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e29c6b671bf0181ecc4226f") } > db.demo74.insertOne({ ... name: "document2", ... data: [{ ...    "Subject": "MongoDB", ...    "isRegular": "Active" ...    }, { ...       "Subject": "MongoDB", ... Read More

Advertisements