Create Performance System to Count Tags in MongoDB

AmitDiwan
Updated on 31-Mar-2020 13:20:59

97 Views

Create an index and then use explain(). Let us create a collection with documents −> db.demo278.ensureIndex({"Subjects":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo278.insertOne({"Subjects":["MySQL", "MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e49096edd099650a5401a55") } > db.demo278.insertOne({"Subjects":["C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e490978dd099650a5401a56") } > db.demo278.insertOne({"Subjects":["Spring", "Hibernate"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e490984dd099650a5401a57") }Display all documents from a collection with the help of find() method −> db.demo278.find();This will produce the following output −{ "_id" : ObjectId("5e49096edd099650a5401a55"), "Subjects" : [ "MySQL", ... Read More

Multi-Key Indexing on an Entire Array with MongoDB

AmitDiwan
Updated on 31-Mar-2020 13:16:35

108 Views

Let us first create a collection with documents −> db.demo277.insertOne({"details":[{"FirstName":"John"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48fb21dd099650a5401a52") } > db.demo277.insertOne({"details":[{"FirstName":"David"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48fb27dd099650a5401a53") } > db.demo277.insertOne({"details":[{"FirstName":"Chris"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48fb2bdd099650a5401a54") }Display all documents from a collection with the help of find() method −> db.demo277.find();This will produce the following output −{ "_id" : ObjectId("5e48fb21dd099650a5401a52"), "details" : [ { "FirstName" : "John" } ] } { "_id" : ObjectId("5e48fb27dd099650a5401a53"), "details" : [ { "FirstName" : "David" } ] } { "_id" : ObjectId("5e48fb2bdd099650a5401a54"), "details" : [ { "FirstName" : ... Read More

Split a Document by Its Subdocuments in MongoDB

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

614 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

Skip First 5 Records and Display Last 5 in MongoDB Query

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

Utilize Indexes When Querying MongoDB Subdocument Without Known Field Names

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

114 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

Insert Long Numbers in MongoDB

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

2K+ 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 Group By to Set Status

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

130 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 Do Not Match a Condition

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

679 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

Query Document in MongoDB Comparing Fields from an Array

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

359 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

Remove Array Element from MongoDB Collection

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

195 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

Advertisements