Found 1659 Articles for Big Data Analytics

Appending an entry in one to many embedded documents with MongoDB

AmitDiwan
Updated on 30-Mar-2020 14:34:24

273 Views

To append an entry in MongoDB, use $push. Let us create a collection with documents −> db.demo253.insertOne( ...   { ...      _id: "101", ...      isActive: false, ...      details: [ ...         { ...            Name: "Chris", ...         }, ...         { ...            CountryName:"US" ...         } ...      ] ...   } ...); { "acknowledged" : true, "insertedId" : "101" }Display all documents from a collection with the help of find() ... Read More

Sort array in MongoDB query and project all fields?

AmitDiwan
Updated on 30-Mar-2020 14:32:20

579 Views

To sort array, use $sort. For projection, use $project in MongoBD aggregate(). Let us create a collection with documents −> db.demo252.insertOne( ...   {"Values" : [ { "v1" : 20, "v2" :30 }, { "v1" : 20, "v2" : 20 }, { "v1" : 10, "v2" : 7 } ] } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c2761627c0c63e7dba78") }Display all documents from a collection with the help of find() method −> db.demo252.find();This will produce the following output −{ "_id" : ObjectId("5e46c2761627c0c63e7dba78"), "Values" : [ { "v1" : 20, "v2" : 30 }, { "v1" : 20, ... Read More

Perform min/max with MongoDB aggregation

AmitDiwan
Updated on 30-Mar-2020 14:28:55

992 Views

For min/max in MongoDB, use $min and $max. Let us create a collection with documents −> db.demo251.insertOne({"Marks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c0001627c0c63e7dba74") } > db.demo251.insertOne({"Marks":87}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c0031627c0c63e7dba75") } > db.demo251.insertOne({"Marks":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c0061627c0c63e7dba76") } > db.demo251.insertOne({"Marks":76}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c00c1627c0c63e7dba77") }Display all documents from a collection with the help of find() method −> db.demo251.find();This will produce the following output −{ "_id" : ObjectId("5e46c0001627c0c63e7dba74"), "Marks" : 78 } { "_id" : ObjectId("5e46c0031627c0c63e7dba75"), "Marks" : 87 } { "_id" : ... Read More

Find the MongoDB collection size for name “Chris”

AmitDiwan
Updated on 30-Mar-2020 14:20:41

63 Views

For this, use bsonsize() in MongoDB. Let us create a collection with documents −> db.demo250.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46bd501627c0c63e7dba70") } > db.demo250.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46bd531627c0c63e7dba71") } > db.demo250.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46bd561627c0c63e7dba72") } > db.demo250.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46bd5b1627c0c63e7dba73") }Display all documents from a collection with the help of find() method −> db.demo250.find();This will produce the following output −{ "_id" : ObjectId("5e46bd501627c0c63e7dba70"), "Name" : "Chris" } { "_id" : ObjectId("5e46bd531627c0c63e7dba71"), "Name" : "Bob" } { "_id" : ObjectId("5e46bd561627c0c63e7dba72"), "Name" ... Read More

Query BinData by Type in MongoDB

AmitDiwan
Updated on 30-Mar-2020 14:10:18

640 Views

To query by type, use subtype() in MongoDB. Let us create a collection with documents −> db.demo249.insertOne({ "_id" : BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") }); {    "acknowledged" : true,    "insertedId" : BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") } > db.demo249.insertOne({"_id" : BinData(4, "CNDF66qIlCY92q1vFAAAAQ==")}); {    "acknowledged" : true,    "insertedId" : UUID("08d0c5eb-aa88-9426-3dda-ad6f14000001") } > db.demo249.insertOne({"_id" : BinData(3, "CNDF66qJ29g92q1vFAAAEw==")}); {    "acknowledged" : true,    "insertedId" : BinData(3, "CNDF66qJ29g92q1vFAAAEw==") }Display all documents from a collection with the help of find() method −> db.demo249.find();This will produce the following output −{ "_id" : BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") } { "_id" : UUID("08d0c5eb-aa88-9426-3dda-ad6f14000001") } { "_id" : BinData(3, "CNDF66qJ29g92q1vFAAAEw==") }Following is ... Read More

MongoDB aggregate to convert multiple documents into single document with an array?

AmitDiwan
Updated on 30-Mar-2020 14:09:09

826 Views

For aggregate in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo248.insertOne({"id":101, "Name":"Chris", "Age":21, "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b6651627c0c63e7dba6d") } > db.demo248.insertOne({"id":101, "Name":"Bob", "Age":22, "CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b6741627c0c63e7dba6e") } > db.demo248.insertOne({"id":102, "Name":"Mike", "Age":20, "CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b6811627c0c63e7dba6f") }Display all documents from a collection with the help of find() method −> db.demo248.find();This will produce the following output −{ "_id" : ObjectId("5e46b6651627c0c63e7dba6d"), "id" : 101, "Name" : "Chris", "Age" : 21, "CountryName" : "US" } { "_id" : ObjectId("5e46b6741627c0c63e7dba6e"), "id" : 101, ... Read More

Find MongoDB documents that contains specific field?

AmitDiwan
Updated on 30-Mar-2020 14:07:18

714 Views

To find documents that contain specific field, use $exists. Let us create a collection with documents −> db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"Chris", "ClientAge":34}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b2a31627c0c63e7dba69") } >db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"John", "ClientLastName":"Smith", "ClientAge":31}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b2be1627c0c63e7dba6a") } > db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"David", "ClientAge":33}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b2cd1627c0c63e7dba6b") } >db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"David", "ClientLastName":"Miller", "ClientAge":31}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b2de1627c0c63e7dba6c") }Display all documents from a collection with the help of find() method −> db.demo247.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e46b2a31627c0c63e7dba69"),    "ClientDetails" : [       {   ... Read More

MongoDB query to skip n first documents?

AmitDiwan
Updated on 30-Mar-2020 13:48:41

108 Views

To skip a specific number of documents, use skip() along with limit. Let us create a collection with documents −> db.demo246.insertOne({"StudentFirstName":"Chris", "StudentLastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b0d71627c0c63e7dba65") } > db.demo246.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b0e21627c0c63e7dba66") } > db.demo246.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b0ea1627c0c63e7dba67") } > db.demo246.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b0f91627c0c63e7dba68") }Display all documents from a collection with the help of find() method −> db.demo246.find();This will produce the following output −{ "_id" : ObjectId("5e46b0d71627c0c63e7dba65"), "StudentFirstName" : "Chris", "StudentLastName" : "Brown" } ... Read More

Sort by subdocument in MongoDB

AmitDiwan
Updated on 30-Mar-2020 13:46:58

658 Views

To sort by subdocument, use $sort in MongoDB. Let us create a collection with documents −> db.demo245.insertOne( ...   { ...      "_id": 101, ...      "deatils": [ ...         { "DueDate": new ISODate("2019-01-10"), "Value": 45}, ...         {"DueDate": new ISODate("2019-11-10"), "Value": 34 } ...      ] ...   } ...); { "acknowledged" : true, "insertedId" : 101 } > db.demo245.insertOne( ...   { ...      "_id": 102, ...      "details": [ ...         { "DueDate": new ISODate("2019-12-11"), "Value": 29}, ...         {"DueDate": ... Read More

Coalesce values from different properties into a single array with MongoDB aggregation

AmitDiwan
Updated on 30-Mar-2020 13:44:46

381 Views

To coalesce values means to merge them. To merge them into a single array, use $project in MongoDB.Let us create a collection with documents −> db.demo244.insertOne({"Value1":10, "Value2":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4582e31627c0c63e7dba63") } > db.demo244.insertOne({"Value1":20, "Value2":30}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4582f11627c0c63e7dba64") }Display all documents from a collection with the help of find() method −> db.demo244.find();This will produce the following output −{ "_id" : ObjectId("5e4582e31627c0c63e7dba63"), "Value1" : 10, "Value2" : 20 } { "_id" : ObjectId("5e4582f11627c0c63e7dba64"), "Value1" : 20, "Value2" : 30 }Following is the query to coalesce values from different properties into ... Read More

Advertisements