MongoDB: Creating, Updating, and Deleting Documents

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

Insert



Insert is the basic method for adding information to MongoDB. In order to add a document to the collection, we do this:

> db.foo.insert( { “bar” : “baz” } );

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

> 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 delete the collection itself or any indexes from it.
You can delete an object using some parameter like this:

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

Deleting is a fairly quick operation, but if you want to clear the collection, it will often be faster to delete the collection itself (drop) and recreate the indexes.

> db.drop_collection( “bar” );

Update



The simplest type of update is a complete replacement for matching documents. There was such a document: If we do like this, then the whole document will be replaced: i.e. will After this request, only the first document found will be updated, about multi-update below.

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




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



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




Modifiers


If we want to increment a field, we use the $ inc modifier. To increase the 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 with the name joe and sets age to 25. It is important that if there is no age key, it will be created.

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

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

Array Modifiers


You can add an element to the array using $ push. Suppose the same joe still has a list of his friends, something like this: We want to add another peter to friends. Such a thing will add Peter to the end of the joe friends array. If there is $ push, then there must be $ pop: This will remove the array element from the end, but this is from the beginning. There is also a useful thing $ addToSet - it 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: This will remove john from friends
}
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"name" : "joe",
"age" : 18,
“friends”: [
{name: “john”},
{name: “helen”}
]
}




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




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



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





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



Positional Array Modifiers


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

For example, the friends list of joe contains not only their friend names, but also their age, and we want to increase the age of a friend under 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 specific friend, for example, john, but we don’t know under which index it lies in the array, we do like this:

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

Upserts


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

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

If our collection does not have a document named “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, you need to add the fourth parameter with the value true to update:

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

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

That's all. These are not all modifiers, only basic ones.
There will be time, there will be a continuation.

Also popular now: