AmitDiwan has Published 10744 Articles

How would I filter out sub documents in MongoDB?

AmitDiwan

AmitDiwan

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

264 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 ...    }, ...    { ...       ... Read More

MongoDB large collection and slow search? How to fix?

AmitDiwan

AmitDiwan

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

443 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 } > ... Read More

How to select maximum item in each group with MongoDB?

AmitDiwan

AmitDiwan

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

398 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" ... Read More

Add field that is unique index to collection in MongoDB?

AmitDiwan

AmitDiwan

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

209 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,   ... Read More

Group by dates in a MongoDB collection?

AmitDiwan

AmitDiwan

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

173 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" : ... Read More

Push and slice multiple times in MongoDB?

AmitDiwan

AmitDiwan

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

240 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" : ... Read More

How to clear a MongoDB database?

AmitDiwan

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 ... Read More

Using regex in MongoDB findOne()

AmitDiwan

AmitDiwan

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

648 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" ... Read More

In MongoDB, is using $in search faster than multiple single searches?

AmitDiwan

AmitDiwan

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

101 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" : ... Read More

Sort the MongoDB documents in ascending order with aggregation?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 09:08:39

214 Views

Use $sort in MongoDB aggregation. Let us create a collection with documents −> db.demo652.insertOne({ ...    value:10, ...    "details" : [{ ...       "ProductName" : "Product-1", ...       "ProductQuantity" : 8, ...       "ProductPrice" : 500 ...    }, { ...     ... Read More

Advertisements