AmitDiwan has Published 10740 Articles

Find document in MongoDB where at least one item from an array is not in the other?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:02:20

187 Views

For this, set regex in MongoDB find(). Let us create a collection with documents −> db.demo228.insertOne({"Subjects":["MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fa51f03d395bdc213473b") } > db.demo228.insertOne({"Subjects":["MongoDB", "Java", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fa52c03d395bdc213473c") }Display all documents from a collection with the help of ... Read More

Updating a set of documents from a list of key value pairs in MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:00:16

475 Views

Let us create a collection with documents −> db.demo227.insertOne({"_id":"101", "Name":"Chris"}); { "acknowledged" : true, "insertedId" : "101" } > db.demo227.insertOne({"_id":"102", "Name":"Bob"}); { "acknowledged" : true, "insertedId" : "102" }Display all documents from a collection with the help of find() method −> db.demo227.find();This will produce the following output −{ "_id" : ... Read More

MongoDB query to display all the values excluding the id?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:58:57

599 Views

For this, use the $project. The $project takes a document that can specify the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting of the values of existing fieldsLet us first create a collection with documents −> db.demo226.insertOne({"Name":"Chris", "Age":21}); {    "acknowledged" ... Read More

Get the last two values with MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:56:58

454 Views

To get the last two values, use MongoDB $slice. Let us create a collection with documents −> db.demo173.insertOne({"ListOfValues":[10, 40, 100, 560, 700, 900]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383a4f9e4f06af551997e4") }Display all documents from a collection with the help of find() method −> db.demo173.find().pretty();This will produce the following ... Read More

Removing item from array in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:55:27

170 Views

To remove item from array, use $pull in MongoDB. Let us create a collection with documents −> db.demo224.insertOne({"ListOfTechnology":["Spring", "Hibernate", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee6d103d395bdc2134733") } > db.demo224.insertOne({"ListOfTechnology":["Groovy"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee6ec03d395bdc2134734") }Display all documents from a collection with the help ... Read More

How to compare multiple properties in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:52:45

343 Views

To compare multiple properties, use $where in MongoDB. Let us create a collection with documents −> db.demo223.insertOne({"Scores":[56, 78]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee4ca03d395bdc2134730") } > db.demo223.insertOne({"Scores":[88, 45]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee4d103d395bdc2134731") } > db.demo223.insertOne({"Scores":[98, 79]}); {    "acknowledged" : true,   ... Read More

MongoDB query to add new array element in document

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:50:24

518 Views

To add new array element in a MongoDB document, use $(projection) along with update(). Let us create a collection with documents −>db.demo222.insertOne({"details":[{"StudentName":"Chris", "StudentMarks":78}, {"StudentName":"David", "StudentMarks":89}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee31703d395bdc213472f") }Display all documents from a collection with the help of find() method −> db.demo222.find().pretty();This will produce ... Read More

MongoDB regular expression to fetch record with specific name “John”, instead of “john”

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:48:48

109 Views

For searching a specific word, use /searchWord/ with regex. Let us create a collection with documents −> db.demo221.insertOne({"Details":{"StudentName":"Chris", "StudentAge":21}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee15d03d395bdc213472b") } > db.demo221.insertOne({"Details":{"StudentName":"John", "StudentAge":20}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee16503d395bdc213472c") } > db.demo221.insertOne({"Details":{"StudentName":"Bob", "StudentAge":22}}); {    "acknowledged" : true, ... Read More

Get all embedded documents with “isMarried” status in a MongoDB collection

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:47:39

234 Views

To get all embedded documents, use $project in MongoDB. Let us create a collection with documents −> db.demo220.insertOne({ ...   "id":101, ...   "FullName" : "John Doe", ...   "EmailId" : "john12@gmail.com", ...   "ShippingDate" : new ISODate(), ...   "details" : { "_id" :1001, "isMarried" :true } ...} ...); ... Read More

Searching a specific domain name from URL records in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:45:07

322 Views

To search for a specific domain name, use /i. Let us create a collection with documents −> db.demo219.insertOne({"details":{"WebsiteURL":"www.EXAMPLE.com"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e667203d395bdc2134718") } > db.demo219.insertOne({"details":{"WebsiteURL":"www.gmail.com"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e667803d395bdc2134719") } > db.demo219.insertOne({"details":{"WebsiteURL":"www.example.com"}}); {    "acknowledged" : true,    "insertedId" : ... Read More

Advertisements