📜 ⬆️ ⬇️

MongoDB: Creating, updating and deleting documents

As part of my reading of the book “MongoDB: The Definitive Guide”, Kristina Chodorow and Michael Dirolf decided to write out the main points from the chapters for better mastering the material. Perhaps someone will be useful too.

Insert



Insert is the base method for adding information to MongoDB. To add a document to the collection, do this:
')
> db.foo.insert( { “bar” : “baz” } );

In such situations, when you need to add several documents, for faster insertion, it is desirable to use the so-called batch insert (group insert)

> db.foo.insert( { “arr” : [ { a : 1 , b : 1 } , { a : 2 , b : 2 } ] } );

Roughly speaking, we simply insert an array.

Remove



All information from the collection can be deleted like this:

> db.users.remove();

This does not remove the collection itself or any indexes from it.
You can delete an object by some parameter like this:

> db.users.remove( { “name” : “Vasya” } );

Deleting is fast enough, but if you want to clear a collection, it will often be faster to delete the collection itself (drop) and re-create the indexes.

> db.drop_collection( “bar” );

Update



The simplest type of update is a complete replacement of matched documents. There was such a document:

}
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"name" : "joe",
"age" : 18
}


If we do this, the whole document will be replaced:

> db.users.update( { “name” : ”joe” } , { “name” : “vasya” } );

those. will be

}
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"name" : "vasya"
}


After this request, only the first found document will be updated, about multi-apdete below.

Modifiers


If we want to increment some field, we use the $ inc modifier. To increase age by 2 years we do this:

> db.users.update( { name: “joe” } , { $inc : { age: 2 } } );

If you need to set the value of a key, use the $ set modifier

> db.users.update( { name:”joe” } , { $set: { age: 25 } } );

It finds a document where the name is joe and sets age to 25. It is important that if there is no age key, then it will be created.

If you need to remove the key, there is a modifier $ unset

> db.users.update( { name: ”joe” } , { $unset: { age : 1 } } );

Array Modifiers


You can add an element to an array using $ push. Suppose this joe still has a list of his friends, something like this:
}
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"name" : "joe",
"age" : 18,
“friends”: [
{name: “john”},
{name: “helen”}
]
}


We want to add more peter'a to friends

> db.users.update( { name: “joe” } , { $push: { friends: { name: “peter” } } } );

Such a thing will add peter'a to the end of joe's array of friends.
If there is $ push, then there must be $ pop:

> db.users.update( { name: ”joe” } , { $pop: { friends: 1 } } );

This will remove the array element from the end, and this from the beginning.

> db.users.update( { name: ”joe” }, { $pop: { friends: -1 } } );

There is also a useful thing $ addToSet - works by analogy with $ push, but with a check for uniqueness.

Sometimes you need to remove an array element by some criterion, then the $ pull modifier is used:

> db.users.update( { }, { $pull: { friends: { name: “john” } } } );

This will remove john's friends.

Positional Array Modifiers


There are 2 ways to manipulate values ​​in an array: by a specific position or by using a positional operator ($ symbol).

For example, in the list of friends joe stores not only their names of friends, but also their age, and we want to increase the age of a friend with the index 0 by 3 years:

> db.users.update( { name: “joe” } , { $inc: { “friends.0.age” : 3 } } );

If we want to increase the age of a particular friend, for example, john, but we don’t know by what index it is in the array, we do this:

> db.users.update( { “friends.name”: “john” } , { $inc: { “friends.$.age” : 3 } } );

Upserts


Upsert is a special type of update. If it is used, if the document on the requested criteria is not found, it will be created, if it is found, then it will be updated, as usual. To use upsert, you just need to add in the update command a third parameter equal to true, like this:

> db.users.update( { name: “helen” } , { $set: { age: 23 } } , true );

If there is no document in our collection with the name “helen”, then it will be created together with the age: 23 field.

Multiple Update


All previous examples using the update command found the first document and updated it. In order to perform a multiple update, add a fourth parameter to the update with the value true:

> db.users.update( { age: 18 } , { $inc: { age: 1 } } , false, true );

This query will find everyone who is 18 years old and will increase it by 1.

In general this is all. These are not all modifiers, only basic ones.
There will be time, there will be a continuation.

Source: https://habr.com/ru/post/134524/


All Articles