Found 1659 Articles for Big Data Analytics

Split a document by its subdocuments in MongoDB

AmitDiwan
Updated on 31-Mar-2020 13:14:32

515 Views

To split a document by its subdocuments, use $unwind in MongoDB. Let us create a collection with documents −> db.demo276.insertOne({"Name":"Chris", "Subjects":["MySQL", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48f953dd099650a5401a51") }Display all documents from a collection with the help of find() method −> db.demo276.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e48f953dd099650a5401a51"),    "Name" : "Chris",    "Subjects" : [       "MySQL",       "MongoDB"    ] }Following is the query to split a document by its subdocuments −> db.demo276.aggregate( [ { $unwind : "$Subjects" } ] )This will produce the following output −{ ... Read More

MongoDB query to skip first 5 records and display only the last 5 of them

AmitDiwan
Updated on 31-Mar-2020 13:12:34

2K+ Views

To skip records in MongoDB, use skip(). With that, to display only a specific number of records, use limit().Let us create a collection with documents −> db.demo275.insertOne({"Number":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48eac4dd099650a5401a43") } > db.demo275.insertOne({"Number":12}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48eac7dd099650a5401a44") } > db.demo275.insertOne({"Number":6}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48eac9dd099650a5401a45") } > db.demo275.insertOne({"Number":1}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48eacadd099650a5401a46") } > db.demo275.insertOne({"Number":5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48eacddd099650a5401a47") } > db.demo275.insertOne({"Number":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48ead0dd099650a5401a48") } > db.demo275.insertOne({"Number":8}); { ... Read More

Can I utilize indexes when querying by MongoDB subdocument without known field names?

AmitDiwan
Updated on 31-Mar-2020 13:10:51

55 Views

Yes, you can achieve this by indexing like “properties.k” for key and “properties.v” for value. The same is used to be implemented in ensureIndex().Let us first see an example and create a collection with documents −> db.demo274.insertOne({"details":[{StudentFirstName:"Chris", StudentLastName:"Brown"}, ...   {StudentFirstName:"David", StudentLastName:"Miller"}, ...   {StudentFirstName:"John", StudentLastName:"Smith"}, ...   {StudentFirstName:"John", StudentLastName:"Doe"} ...] ...} ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48de35dd099650a5401a42") }Display all documents from a collection with the help of find() method −> db.demo274.find().pretty();OutputThis will produce the following output −{    "_id" : ObjectId("5e48de35dd099650a5401a42"),    "details" : [       {          "StudentFirstName" : ... Read More

How to insert LONG numbers in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 13:07:14

1K+ Views

To insert LONG numbers, use NumberLong(). It is used to handle 64-bit integers. Let us create a collection with documents −> db.demo273.insert({ ...   Name:"Robert", ...   id: NumberLong("100000000000001"), ...   isActive:true ...}) WriteResult({ "nInserted" : 1 }) > db.demo273.insert({ ...   Name:"David", ...   id: NumberLong("98888888888888888"), ...   isActive:false ...}) WriteResult({ "nInserted" : 1 }) > db.demo273.insert({ ...   Name:"Mike", ...   id: NumberLong("999999999999"), ...   isActive:true ...}) WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo273.find();OutputThis will produce the following output −{ "_id" : ObjectId("5e4824df1627c0c63e7dbac3"), "Name" : "Robert", "id" ... Read More

MongoDB GroupBy to set status

AmitDiwan
Updated on 31-Mar-2020 12:55:01

85 Views

For this, you can use aggregate() in MongoDB. Let us create a collection with documents −> db.demo149.insertOne({"Status":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350386fdf09dd6d08539c4") } > db.demo149.insertOne({"Status":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350388fdf09dd6d08539c5") } > db.demo149.insertOne({"Status":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e35038afdf09dd6d08539c6") }Display all documents from a collection with the help of find() method −> db.demo149.find();This will produce the following output −{ "_id" : ObjectId("5e350386fdf09dd6d08539c4"), "Status" : 40 } { "_id" : ObjectId("5e350388fdf09dd6d08539c5"), "Status" : 40 } { "_id" : ObjectId("5e35038afdf09dd6d08539c6"), "Status" : 50 }Here is the query to MongoDB group by ... Read More

Find records in MongoDB that does NOT match a condition?

AmitDiwan
Updated on 31-Mar-2020 12:51:00

575 Views

To find records that does not match a condition, use $ne. Let us create a collection with documents −> db.demo148.insertOne({"Message":"Hello"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32fb37fdf09dd6d08539c0") } > db.demo148.insertOne({"Message":"Good"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32fb3efdf09dd6d08539c1") } > db.demo148.insertOne({"Message":"Bye"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32fb42fdf09dd6d08539c2") }Display all documents from a collection with the help of find() method −> db.demo148.find();This will produce the following output −{ "_id" : ObjectId("5e32fb37fdf09dd6d08539c0"), "Message" : "Hello" } { "_id" : ObjectId("5e32fb3efdf09dd6d08539c1"), "Message" : "Good" } { "_id" : ObjectId("5e32fb42fdf09dd6d08539c2"), "Message" : "Bye" }Following is the query to ... Read More

How to query a document in MongoDB comparing fields from an array?

AmitDiwan
Updated on 31-Mar-2020 12:49:06

309 Views

To compare fields from an array, use $gt and $lt. Let us create a collection with documents −> db.demo147.insertOne({"Details":[{"Score":45}, {"Score":46}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32fa21fdf09dd6d08539be") } > db.demo147.insertOne({"Details":[{"Score":65}, {"Score":86}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32fa40fdf09dd6d08539bf") }Display all documents from a collection with the help of find() method −> db.demo147.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e32fa21fdf09dd6d08539be"),    "Details" : [       {          "Score" : 45       },       {          "Score" : 46       } ... Read More

Removing an array element from a MongoDB collection

AmitDiwan
Updated on 31-Mar-2020 12:47:11

127 Views

To remove an array element, simply use $pull along with update(). Let us create a collection with documents −> db.demo146.insertOne({"ListOfEmployeeNames":["Chris", "David", "Bob", "Mike"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32f54ffdf09dd6d08539bd") }Display all documents from a collection with the help of find() method −> db.demo146.find();This will produce the following output −{ "_id" : ObjectId("5e32f54ffdf09dd6d08539bd"), "ListOfEmployeeNames" : [ "Chris", "David", "Bob", "Mike" ] }Following is the query to remove an array element from MongoDB −> db.demo146.update({}, { "$pull": { "ListOfEmployeeNames": "Bob" } }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with ... Read More

Alternative of MongoDB operator $eq to get similar result

AmitDiwan
Updated on 31-Mar-2020 12:45:12

176 Views

For writing an equality, you can simply use find() along with match value. Let us create a collection with documents −> db.demo145.insertOne({"ListOfNames":["Chris", "David", "Mike"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32f37bfdf09dd6d08539bb") } > db.demo145.insertOne({"ListOfNames":["Bob", "John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32f384fdf09dd6d08539bc") }Display all documents from a collection with the help of find() method −> db.demo145.find();This will produce the following output −{ "_id" : ObjectId("5e32f37bfdf09dd6d08539bb"), "ListOfNames" : [ "Chris", "David", "Mike" ] } { "_id" : ObjectId("5e32f384fdf09dd6d08539bc"), "ListOfNames" : [ "Bob", "John" ] }Following is the query to implement MongoDB operator $eq −> db.demo145.find({"ListOfNames":"John"});This will produce ... Read More

Find result within array of objects and match email address field in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 12:43:45

238 Views

Let us first create a collection with documents −>db.demo144.insertOne({"EmployeeDetails":[{"EmployeeName":"Chris", "EmployeeEmail":"Chris12@gmail.com"}, {"EmployeeName":"Bob", "EmployeeEmail":"bo22@gmail.com"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32f1d8fdf09dd6d08539b9") } >db.demo144.insertOne({"EmployeeDetails":[{"EmployeeName":"David", "EmployeeEmail":"david@gmail.com"}, {"EmployeeName":"Carol", "EmployeeEmail":"Carol@gmail.com"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32f1f5fdf09dd6d08539ba") }Display all documents from a collection with the help of find() method −> db.demo144.find();This will produce the following output −{    "_id" : ObjectId("5e32f1d8fdf09dd6d08539b9"), "EmployeeDetails" : [       { "EmployeeName" : "Chris", "EmployeeEmail" : "Chris12@gmail.com" },       { "EmployeeName" : "Bob", "EmployeeEmail" : "bo22@gmail.com" }    ] } {    "_id" : ObjectId("5e32f1f5fdf09dd6d08539ba"), "EmployeeDetails" : [       { "EmployeeName" ... Read More

Advertisements