Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are MongoDB queries client-side operations?

Lets say I have a document

{ "_id" : ObjectId("544946347db27ca99e20a95f"), "nameArray": [{"id":1 , first_name: "foo"}]

Now i need to push a array into nameArray using $push . How does document update in that case. Does document get's retrieved on client and updates happens on client and changes are then reflected to Mongodb database server. Entire operation is carried out in Mongodb Database.

like image 445
Priyank Bhatt Avatar asked May 11 '16 07:05

Priyank Bhatt


People also ask

What are MongoDB queries?

What is MongoDB Query? MongoDB Query is a way to get the data from the MongoDB database. MongoDB queries provide the simplicity in process of fetching data from the database, it's similar to SQL queries in SQL Database language.

Which query is used to perform the operation in MongoDB?

The find() Method To query data from MongoDB collection, you need to use MongoDB's find() method.

Does MongoDB support query?

You can use the MongoDB Shell to query and update data as well as perform administrative operations.

Where is operation in MongoDB?

The MongoDB $where operator is used to match documents that satisfy a JavaScript expression. A string containing a JavaScript expression or a JavaScript function can be pass using the $where operator. The JavaScript expression or function may be referred as this or obj.


2 Answers

What you are asking here is if MongoDB operations are client-side operations. The short answer is NO.

In MongoDB a query targets a specific collection of documents as mentioned in the documentation and a collection is a group of MongoDB documents which exists within a single database. Collections are simply what tables are in RDBMS. So if query targets a specific collection then it means their are perform on database level, thus server-side. The same thing applies for data modification and aggregation operations.

Sometimes, your operations may involve a client-side processing because MongoDB doesn't provides a way to achieve what you want out of the box. Generally speaking, you only those type of processing when you want to modify your documents structure in the collection or change your fields' type. In such situation, you will need to retrieve your documents, perform your modification using bulk operations.

like image 145
styvane Avatar answered Oct 03 '22 16:10

styvane


See the documentation:

Your array is inserted into the existing array as one element. If the array does not exists it is created. If the target is not an array the operation fails.

There is nothing stated like "retriving the element to the client and update it there". So the operation is completely done on the database server side. I don't know any operation that works in the way like you described it. Unless you are chaining a query, with a modify of the item in your client and an update. But these are two separated operations and not one single command.

like image 22
Simulant Avatar answered Oct 03 '22 18:10

Simulant