MongoDB - Update objects in a document's array | MongoDB update object in Array

Sometime in MongoDB, we need to update the object in the array for the particular matching document. We need to update either the first matching object or all the matching objects in the array. Let me give you an example of the documents. Let say I have the following collection whose name is items.

Now I wanted to update the latest price of the item whose name is "my_item_two".

We achieve this through the positional operator ($). The positional operator is used to performing the operation on the particular position. Let me write the query for that:

This will update the first matching array element but sometime we may need to update all the matching array elements and we can do that by using the $[]. The all positional operator $[] indicates that the update operator should modify all elements in the specified array field.

  ...  Read More

Share This:


MongoDB find match in array of objects || MongoDB where condition with array of objects | $elemMatch or dot notation match

In MongoDB, $elemMatch and dot notation are used to find the match in the array of objects. But both have some difference.
Let me explain this with an example. Let say we have a document like:

Now let me find the class name whose students has scored greater than 86.

If using the dot notation then here is the example:

The result will be :

Now If I find the class name whose students has scored greater than 86 and their section is A by using dot notation.

The result will be:

Hence if we are using dot notation, Any condition match in students array, like score or section will be returning document.



But what if we wanted to match every condition?  The answer is: We use $elemMatch for that. ...  Read More

Share This:


Push multiple objects into MongoDB array document | Append Multiple Objects to an Array in MongoDB

Normally we use $push to push or append the element or object into an array in mongoDB. But sometime we may need to push the multiple objects into mongoDB array document. Here below I'll try to explain how we can do that.

Let say I have a collection of favorite products and the collection name is favorite_products.

Now in the above document, I wanted to push/append the multiple user objects into the users array whose product id is 124.



 ...  Read More

Share This:


MongoDB distinct with array of objects | Getting distinct values from object array MongoDB

Let say we have a collection transactions which contains some document and documents contain payments which is array of objects.
Now my task is to get the unique types in payments array of every document.

Expected Result: Debit Card, Internet Banking, Credit Card, UPI



which is unique values of payment types in transctions collection and the MongoDB query for that is given below:

Here we first unwind the data so that every payments array can be converted to objects and then we are grouping the type of payments document in transctions collection. ...  Read More

Share This:


Create or Insert Operation in MongoDB

a) db.collection.insert() - Version 2.6

b) db.collection.save() - Version 2.6  

The save() method uses either the insert or the update command. If record already exit the it updates else insert the new document.

c) db.collection.insertOne() - Version 3.2

d) db.collection.insertMany() - Version 3.2

 

Share This:


What is the use of MongoDB or When to use MongoDB ?

  • MongoDB is mostly used as the primary data store for operational applications with real-time requirements . MongoDB is easy to operate and scale in ways that are hard if not impossible with relational databases.
  • In many use cases where relational databases aren’t a good fit, like applications with unstructured, semi-structured and polymorphic data, as well as applications with large scalability requirements or multi-data center deployments so in that case MongoDB is the best option to choose.
  • MongoDB may not be a good fit for some applications. For example, applications that require complex transactions  and scan-oriented applications that access large subsets of the data most of the time may not be a good fit for MongoDB. MongoDB is not a drop-in replacement for legacy applications built around the relational data model and SQL.
  • Some common use cases include mobile apps, product catalogs, real-time personalization, content management, logging and applications delivering a single view across multiple systems.
  •  ...  Read More

    Share This:


    Node JS with mongoose | Create schema | Save data to mongodb in node js

    To install the mongoose:                                                                    

    routes/post.js

    MongoDB connection using mongoose with node JS: Write the following code in app.js Create schema for blog post: schema/post.js View : views/post.js

    Download Code

    Share This:


    What is MongoDB?

    What is MongoDB? and Why MongoDB?

    MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document. MongoDB is leading NoSQL database which is written in C++ and stores data in the form JSON.

    Share This:


    How to install mongo db on mac

    1)To install brew

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    Or to update brew

    brew update

    2) To install mongodb

    brew install mongodb

    Share This: