Perform Bulk Insert in MongoDB

AmitDiwan
Updated on 13-May-2020 09:35:28

429 Views

For bulk insert in MongoDB, use initializeUnorderedBulkOp(). Let us create a collection with documents −> var bulkInsertDoc = db.demo663.initializeUnorderedBulkOp(); > bulkInsertDoc.insert( { Name: "John", CountryName:"US"} ); > bulkInsertDoc.insert( { Name: "Chris", CountryName:"UK"} ); > bulkInsertDoc.insert( { Name: "David", CountryName:"AUS"} ); > bulkInsertDoc.execute(); BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" : [ ],    "nInserted" : 3,    "nUpserted" : 0,    "nMatched" : 0,    "nModified" : 0,    "nRemoved" : 0,    "upserted" : [ ] })Display all documents from a collection with the help of find() method −> db.demo663.find();This will produce the following output −{ "_id" : ... Read More

Filter Out Sub-Documents in MongoDB

AmitDiwan
Updated on 13-May-2020 09:33:02

266 Views

To filter out sub documents, use MongoDB aggregate and in that, use $unwind. Let us create a collection with documents −> db.demo662.insertOne( ... { ...    "details":[ ...   { ...       Name:"Chris", ...       Marks:35 ...    }, ...    { ...       Name:"Bob", ...       Marks:45 ...    }, ...    { ...       Name:"David", ...       Marks:30 ...    } ... ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1b2be24113ea5458c7d04") }Display all documents from a collection with the help of ... Read More

Fix Slow Search Issues in Large MongoDB Collections

AmitDiwan
Updated on 13-May-2020 09:29:44

449 Views

For faster search, create index. For this, use createIndex(). Let us create a collection with documents −> db.demo661.createIndex({ListOfName:1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo661.insertOne({_id:1, ListOfName:["John", "Robert", "David"]}); { "acknowledged" : true, "insertedId" : 1 } > db.demo661.insertOne({_id:2, ListOfName:["Mike", "Sam"]}); { "acknowledged" : true, "insertedId" : 2 } > db.demo661.insertOne({_id:3, ListOfName:["John", "David", "Bob"]}); { "acknowledged" : true, "insertedId" : 3 }Display all documents from a collection with the help of find() method −> db.demo661.find();This will produce the following output −{ "_id" : 1, "ListOfName" : [ "John", ... Read More

Select Maximum Item in Each Group with MongoDB

AmitDiwan
Updated on 13-May-2020 09:24:56

403 Views

You can use $group. Let us create a collection with documents −> db.demo659.insertOne({Name:"Chris", CountryName:"US", "Marks":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50724113ea5458c7cf9") } > db.demo659.insertOne({Name:"David", CountryName:"US", "Marks":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50724113ea5458c7cfa") } > db.demo659.insertOne({Name:"Mike", CountryName:"US", "Marks":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50724113ea5458c7cfb") } > db.demo659.insertOne({Name:"Chris", CountryName:"UK", "Marks":75}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50724113ea5458c7cfc") } > db.demo659.insertOne({Name:"David", CountryName:"UK", "Marks":54}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50724113ea5458c7cfd") } > db.demo659.insertOne({Name:"Mike", CountryName:"UK", "Marks":72}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50824113ea5458c7cfe") }Display all documents from a collection ... Read More

Add Unique Index Field to Collection in MongoDB

AmitDiwan
Updated on 13-May-2020 09:23:06

212 Views

For unique index, set unique − true while creating an index. Let us create a collection with documents −> db.demo658.createIndex({FirstName:1}, {unique:true, sparse:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > > db.demo658.insertOne({"FirstName":"John", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea067144deddd72997713d9") } > db.demo658.insertOne({"FirstName":"Adam", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea0671c4deddd72997713da") } > db.demo658.insertOne({"FirstName":"John", "LastName":"Doe"}); 2020-04-22T21:17:46.072+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: onlinecustomertracker.demo658 index: FirstName_1 dup key: { : "John" } : WriteError({    "index" : 0,    "code" : 11000,   ... Read More

Group By Dates in a MongoDB Collection

AmitDiwan
Updated on 13-May-2020 09:22:22

180 Views

To group by dates, use $group in MongoDB aggregate. Let us create a collection with documents −> db.demo657.insertOne({ ...       id: 1, ...       Name: "Chris", ...       DueDate: new ISODate("2020-04-22") ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea064b44deddd72997713d6") } > db.demo657.insertOne( ... { ...    id: 1, ...    Name: "John", ...    DueDate: new ISODate("2020-04-22") ... } ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea064b44deddd72997713d7") } > db.demo657.insertOne( ... { ...    id: 1, ...    Name: "Chris", ...    DueDate: new ISODate("2020-04-22") ... ... Read More

Push and Slice Multiple Times in MongoDB

AmitDiwan
Updated on 13-May-2020 09:16:21

248 Views

To push and slice in MongoDB, use $push and $slice. Let us create a collection with documents −> db.demo656.insertOne({Name:"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea060264deddd72997713cf") }Display all documents from a collection with the help of find() method −> db.demo656.find();This will produce the following output −{ "_id" : ObjectId("5ea060264deddd72997713cf"), "Name" : "John" }Here is the query to push and slice in MongoDB−> db.demo656.update({Name:"John"}, {"$push":{"ListOfName": {"$each": ["John"], "$slice": -9}}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with the help of find() method −> db.demo656.find();This will produce the following output −{ ... Read More

Clear a MongoDB Database

AmitDiwan
Updated on 13-May-2020 09:14:16

2K+ Views

To clear, use dropDatabase. Following is the syntax −use yourDatabaseName; db.dropDatabase();To clear a MongoDB database, first show all the databases −> show dbsThis will produce the following output −MyDB    0.000GB admin    0.000GB config    0.000GB local    0.000GB onlinecustomertracker 0.000GB test    0.006GBNow, let us delete the database onlinecustomertracker −> use onlinecustomertracker switched to db onlinecustomertracker > db.dropDatabase(); { "dropped" : "onlinecustomertracker", "ok" : 1 }Following is the query to show all databases after deleting a database above −> show dbsThis will produce the following output −MyDB    0.000GB admin    0.000GB config    0.000GB local    0.000GB test    0.006GB

Using Regex in MongoDB findOne

AmitDiwan
Updated on 13-May-2020 09:12:23

655 Views

The findOne() returns one document that satisfies the specified query criteria on the collection. Let us create a collection with documents −> db.demo655.insertOne({subject:"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea050254deddd72997713cc") } > db.demo655.insertOne({subject:"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea0502b4deddd72997713cd") } > db.demo655.insertOne({subject:"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea050314deddd72997713ce") }Display all documents from a collection with the help of find() method −> db.demo655.find();This will produce the following output −{ "_id" : ObjectId("5ea050254deddd72997713cc"), "subject" : "MySQL" } { "_id" : ObjectId("5ea0502b4deddd72997713cd"), "subject" : "MongoDB" } { "_id" : ObjectId("5ea050314deddd72997713ce"), "subject" : "Java" }Following is ... Read More

Is Using $in Search Faster than Multiple Single Searches in MongoDB?

AmitDiwan
Updated on 13-May-2020 09:09:59

106 Views

Yes, using $in is faster. Let us see an example and create a collection with documents −> db.demo653.insertOne({subject:"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea04b274deddd72997713c0") } > db.demo653.insertOne({subject:"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea04b304deddd72997713c1") } > db.demo653.insertOne({subject:"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea04b354deddd72997713c2") } > db.demo653.insertOne({subject:"C"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea04b384deddd72997713c3") } > db.demo653.insertOne({subject:"C++"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea04b3b4deddd72997713c4") }Display all documents from a collection with the help of find() method −> db.demo653.find();This will produce the following output −{ "_id" : ObjectId("5ea04b274deddd72997713c0"), "subject" : ... Read More

Advertisements