0% found this document useful (0 votes)
348 views

Unit - 5 - Chapter 3 - Creating, Updating, and Deleting Documents in MongoDB

Here are the queries for the scenarios: 1. db.post.find({title: "How Delhi cops quickly ended the riots"}) 2. db.post.find({likes: {$lt: 100}}) 3. db.post.find({likes: {$lte: 100}}) 4. db.post.find({likes: {$gt: 100}}) db.post.find({likes: {$gte: 100}}) db.post.find({likes: {$ne: 100}}) The $lt, $lte, $gt, $gte and $ne operators are used to check for less than, less than or equal to, greater than, greater than

Uploaded by

Shamanth Edge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
348 views

Unit - 5 - Chapter 3 - Creating, Updating, and Deleting Documents in MongoDB

Here are the queries for the scenarios: 1. db.post.find({title: "How Delhi cops quickly ended the riots"}) 2. db.post.find({likes: {$lt: 100}}) 3. db.post.find({likes: {$lte: 100}}) 4. db.post.find({likes: {$gt: 100}}) db.post.find({likes: {$gte: 100}}) db.post.find({likes: {$ne: 100}}) The $lt, $lte, $gt, $gte and $ne operators are used to check for less than, less than or equal to, greater than, greater than

Uploaded by

Shamanth Edge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

CREATING, UPDATING

AND
DELETING DOCUMENTS
Objectives
• This
chapter covers the basics of moving data in and out of the
database, including the following:
• Adding new documents to a collection
• Removing documents from a collection
• Updating existing documents
• Choosing the correct level of safety versus speed for all of these
operations
Inserting and Saving Documents
• insert()
• It is the basic method for adding data to MongoDB.
• To insert a document into a collection
• If the collection does not exist, then this method will create the collection
• If the document does not specify an _id field, then MongoDB will add the _id field
and assign a unique ObjectId for the document before inserting.
• Most drivers create an ObjectId and insert the _id field, but the mongod will
create and populate the _id if the driver or application does not.
• If the document contains an _id field, the _id value must be unique within the
collection to avoid duplicate key error.
• Eg:
db.bios.insert({“age”: 23})

This will add an _id key to the document if it does not exist and stores into the
MongoDB
db.products.insert( { item: "card", qty: 15 } )
During the insert, mongod will create the _id field and assign it a unique
ObjectId value, as verified by the inserted document as shown below:

{ "_id" : ObjectId("5063114bd386d8fadbd6b004"), "item" : "card", "qty" : 15 }


db.products.insert( { _id: 10, item: "box", qty: 20 } )
After the insert the document looks like this
{ "_id" : 10, "item" : "box", "qty" : 20 }
What is the output of this code?
db.products.insert(
[
{ _id: 11, item: "pencil", qty: 50, type: "no.2" },
{ item: "pen", qty: 20 },
{ item: "eraser", qty: 25 }
]
)
Output of the ordered insert
{ "_id" : 11, "item" : "pencil", "qty" : 50, "type" : "no.2" }
{ "_id" : ObjectId("51e0373c6f35bd826f47e9a0"), "item" : "pen",
"qty" : 20 }
{ "_id" : ObjectId("51e0373c6f35bd826f47e9a1"), "item" : "eraser",
"qty" : 25 }
• By default, MongoDB performs an ordered insert.
• With ordered inserts, if an error occurs during an insert of one of the
documents, MongoDB returns on error without processing the remaining
documents in the array.
Perform an Unordered Insert
• The following example performs an unordered insert of three documents.
• With unordered inserts, if an error occurs during an insert of one of the
documents, MongoDB continues to insert the remaining documents in the
array.
db.products.insert(
[
{ _id: 20, item: "lamp", qty: 50, type: "desk" },
{ _id: 21, item: "lamp", qty: 20, type: "floor" },
{ _id: 22, item: "bulk", qty: 100 }
],
{ ordered: false }
)
Batch Insert
• If you have a situation where you are inserting multiple documents into a
collection, you can make the insert faster by using batch inserts.
• Batch inserts allow you to pass an array of documents to the database.
• In the shell, you can try this out using the batchInsert function, which is similar
to insert except that it takes an array of documents to insert:
db.foo.batchInsert([{"_id" : 0}, {"_id" : 1}, {"_id" : 2}])
> db.foo.find()
{ "_id" : 0 }
{ "_id" : 1 }
{ "_id" : 2 }
• Sending dozens, hundreds, or even thousands of documents at a time can make
inserts significantly faster.
Is there a limit for batch insert?
• Current versions of MongoDB do not accept messages longer than 48 MB, so
there is a limit to how much can be inserted in a single batch insert.
• If you attempt to insert more than 48 MB, many drivers will split up the batch
insert into multiple 48 MB batch inserts.
*Check your driver documentation for details.
Insert Validation

• MongoDB does minimal checks on data being inserted:


• it check’s the document’s basic structure and adds an "_id" field if one does not exist.
• One of the basic structure checks is size:
• all documents must be smaller than 16 MB (Check the recent release document)
• This is a somewhat arbitrary limit (and may be raised in the future); it is mostly to prevent
bad schema design and ensure consistent performance.
• To see the BSON size (in bytes) of the document doc
• run Object.bsonsize(doc) from the shell
• What is the impact of the check for inserting data?

• These minimal checks also mean that it is fairly easy to insert invalid data
• Thus, you should only allow trusted sources, such as your application servers,
to connect to the database.
• All of the drivers for major languages do check for a variety of invalid data
(documents that are too large, contain non-UTF-8 strings, or use unrecognized
types) before sending anything to the database.
SAMPLE 100 YEAR
WEATHER
MONITORING DATA
Sample Results data
Sample movies collection data
Consider the following code:
>db.mycol.insert({ • Identify the collection
name
_id: ObjectId(7df78ad8902c),
• If the collection does not
title: 'MongoDB Overview', exist then what does the
description: 'MongoDB is no sql database', Mongo do?
by: 'tutorials point', • What happens if we do
not specify the id?
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
• To insert multiple documents in a single query, you can pass an array of documents in insert() command.
>db.post.insert([
{
title: 'MongoDB Overview', description: 'MongoDB is no sql database', by: 'tutorials point', url:
'http://www.tutorialspoint.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 },
{
title: 'NoSQL Database', description: "NoSQL database doesn't have tables", by: 'tutorials point',
url: http://www.tutorialspoint.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])
• Saving the document
• To insert the document you can use db.post.save(document) also.
• If you don't specify _id in the document then save() method will work
same as insert() method.
• If you specify _id then it will replace whole data of document containing _id
as specified in save() method.
Removing Documents
• > db.bios.remove()
• This will remove all of the documents in the bios collection.
• This doesn’t actually remove the collection, and any meta information about it
will still exist.
• The remove function optionally takes a query document as a parameter.
• When it’s given, only documents that match the criteria will be removed.
• Suppose, for instance, that we want to remove everyone from the mailing.list collection
where the value for "opt-out “is true:
• > db.mailing.list.remove({"opt-out" : true})

• Once data has been removed, it is gone forever.


• There is no way to undo the remove or recover deleted
documents.
remove() versus drop()
• Removing documents is usually a fairly quick operation, but if you want to
clear an entire collection, it is faster to drop it.
• There was an experiment conducted with we have one million
dummy records created and then deleted using remove functions, it
was found that On a MacBook Air, this script prints “Remove took:
9676ms”.
• If the remove and findOne are replaced by drop(), the time drops to one
millisecond!
• This is obviously a vast improvement, but it comes at the expense of
granularity: we cannot specify any criteria.
• The whole collection is dropped, and all of its metadata is deleted.
• Code fro inserting dummy records
>for (var i = 0; i < 1000000; i++) {
... db.tester.insert({"foo": "bar", "baz": i, "z": 10 - i})
... }
• Code to remove all of the documents we just inserted, measuring the time it takes.
First, here’s a simple remove:
> var timeRemoves = function() {
... var start = (new Date()).getTime();
...
... db.tester.remove();
... db.findOne(); // makes sure the remove finishes before continuing
...
... var timeDiff = (new Date()).getTime() - start;
... print("Remove took: "+timeDiff+"ms");
... }
> timeRemoves()
MongoDB - Query Document
• This is part of Read operation in MongoDB
• Read operations retrieves documents from a collection; i.e. queries a collection
for documents.
• The find() Method
• Helps in querying the data from MongoDB collection
• Method will display all the documents in a non-structured way.
• Syntax
• >db.COLLECTION_NAME.find()

• The pretty() Method


• To display the results in a formatted way, you can use pretty() method.
• Syntax
• >db.mycol.find().pretty()
Check the following code with the modifier

You can specify query filters or criteria that identify the documents to return.


Write the query for the following scenario
1. In the blog post example find the document where post with the title “ How
Delhi cops quickly ended the riots”
2. Find the document where the like for this news is less than 100
3. Find the document where the like for this news is less or equal to 100
4. Try for greater than, greater than or equal to and not equal to
use $gt, $gte, $ne
1. db.post.find({“title”: “How Delhi cops quickly ended the riots”}).pretty()
2. db.post.find({"likes":{$lt:100}}).pretty()
3. Less than or equal can be mentioned as
db.post.find({"likes":{$lte:100}}).pretty()
Let us consider the following example
>db.createCollection(“books")
db.books.insert(
{ _id: ObjectId(7df78ad8902c),
title: ‘MongoDB : A definitive guide’,
description: 'MongoDB is no sql database’,
by: ‘Cristina C’,
url: ‘http://wrox.com’,
tags: ['mongodb', 'database', 'NoSQL'],
downloads: 1000 })
Add few documents to the collection..
Let us check the working of AND, OR conditions on this documents
AND in MongoDB

• Syntax
• In the find() method, if you pass multiple keys by separating them by ',' then
MongoDB treats it as AND condition.
• Following is the basic syntax of AND −
• >db.mycol.find({ $and: [ {key1: value1}, {key2:value2} ] } ).pretty()
OR in MongoDB

• Syntax
• To query documents based on the OR condition, you need to use $or keyword.
Following is the basic syntax of OR −
• >db.mycol.find( { $or: [ {key1: value1}, {key2:value2} ] } ).pretty()
• Query
• Find all the books written by “Cristina” and whose title is “MongoDB : A definitive guide”

• db.books.find({$and:[
{"by":“Cristina"},
{"title": " MongoDB : A definitive guide "}
]}).pretty()
• You can pass any number of key, value pairs in find clause

• Find all the books with the title “MongoDB Overview” or written by “Cristina”

• db.books.find({$or:[{"by":“Cristina"},
{"title": " MongoDB : A definitive guide "}]}).pretty()

• Find all the documents that have download greater than 10 and whose title is either MongoDB : A
definitive guide ' or written by ‘Cristina’
• db.books.find({“downloads": {$gt:500}, $or: [{"by": “Cristina"}, {"title": " MongoDB : A
definitive guide"}]}).pretty()
Updating Documents

• Once a document is stored in the database, it can be changed using the update
method.
• The update takes two parameters:
• a query document, which locates documents to update
• a modifier document, which describes the changes to make to the
documents found.
• Updating a document is atomic:
• if two updates happen at the same time, whichever one reaches the server first will be
applied, and then the next one will be applied.
• Thus, conflicting updates can safely be sent in rapid-fire succession without any
documents being corrupted: the last update will “win.”
• Consider the inventory collection shown below:

• db.inventory.insertMany( [
• { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
• { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
• { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
• { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
• { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
• { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
• { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
• { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
• { item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
• { item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" } ] );
• Now let us work with update operator
• Update Documents in a Collection

• To update a document, MongoDB provides update operators, such as $set, to


modify field values.
• To use the update operators, pass to the update methods an update document of
the form:
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... }, ...
}
Update a Single Document
• The following example uses the db.collection.updateOne() method on
the inventory collection to update the first document here item equals "paper":
• db.inventory.updateOne(
{ item: "paper" },
{ $set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true } } )
• The update operation:
• uses the $set operator to update the value of the size.uom field to "cm" and the value of
the status field to "P",
• uses the $currentDate operator to update the value of the lastModified field to the
current date.
• if lastModified field does not exist, $currentDate will create the field. See $currentDate
 for details
Document Replacement
• The simplest type of update fully replaces a matching document with a new
one.
• db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
Consider the mycol collection has the following data.
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point
Overview“}
Now if we need to update the title of the document with the new value 'New MongoDB
Tutorial' of the documents whose title is 'MongoDB Overview'.
• db.mycol.update({'title':'MongoDB Overview'},
{$set:{'title':'New MongoDB Tutorial'}})
• >db.mycol.find()
• { "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}
• { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
• { "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
How do we update for joe with age 20
• db.people.find()
• {"_id" : ObjectId("4b2b9f67a1f631733d917a7b"), "name" : "joe", "age" : 65},
• {"_id" : ObjectId("4b2b9f67a1f631733d917a7c"), "name" : "joe", "age" : 20},
• {"_id" : ObjectId("4b2b9f67a1f631733d917a7d"), "name" : "joe", "age" : 49},

• What happens when we do this ?


• db.people.update({"name" : "joe"}, joe);
• Previous update leads to this error
• E11001 duplicate key on update
• Best way to update by id
• > db.people.update({"_id" : ObjectId("4b2b9f67a1f631733d917a7c")},
joe)
PART-II
Using Modifiers
Using Modifiers
• Usually only certain portions of a document need to be updated.
• You can update specific fields in a document using atomic update modifiers.
• Update modifiers are special keys that can be used to specify complex update operations,
such as altering, adding, or removing keys, and even manipulating arrays and
embedded documents.
• $inc
• $set
• $unset
Let us consider the following example
• Suppose we were keeping website analytics in a collection and wanted to increment a counter
each time someone visited a page. We can use update modifiers to do this increment
atomically. Each URL and its number of page views is stored in a document that looks like this:
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"url" : "www.example.com",
"pageviews" : 52
}
• Every time someone visits a page, we can find the page by its URL and use the "$inc“
modifier to increment the value of the "pageviews" key:
• > db.analytics.update({"url" : "www.example.com"},
• ... {"$inc" : {"pageviews" : 1}})
• Now, if we do a find, we see that "pageviews" has increased by one:
• > db.analytics.find()
• Every time someone visits a page, we can find the page by its URL and use the "$inc“
modifier to increment the value of the "pageviews" key:
• > db.analytics.update({"url" : "www.example.com"},
• ... {"$inc" : {"pageviews" : 1}}) // inc – increment operator
• Now, if we do a find, we see that "pageviews" has increased by one:
• > db.analytics.find()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"url" : "www.example.com",
"pageviews" : 53
}
• When using modifiers, the value of "_id" cannot be changed.
• Values for any other key, including other uniquely indexed keys, can be modified.
Understanding $set modifier
• The "$set" sets the value of a field.
• If the field does not yet exist, it will be created. This can be handy for updating schema
or adding user-defined keys.
• For example, suppose you have a simple user profile stored as a document that looks
something like the following:
> db.users.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "joe",
"age" : 30,
"sex" : "male",
"location" : "Wisconsin"
}
• If the user wanted to store his favorite book in
his profile, he could add it using "$set": • Observe that the document will have a
“favorite book” key:
 db.users.update(
> db.users.findOne()
{"_id" : { "_id" : ObjectId("4b253b067525f35f94b60a31"),
ObjectId("4b253b067525f35f94b60a31")},
"name" : "joe",
... {"$set" : {"favorite book" : "War and Peace"}})
"age" : 30,

"sex" : "male",

"location" : "Wisconsin",

"favorite book" : "War and Peace"

}
• Now if joe decides that he actually enjoys a different book, "$set" can be used
again to change the value:
• > db.users.update({"name" : "joe"},
• ... {"$set" : {"favorite book" : "Green Eggs and Ham"}})
• Now if our fickle joe decides that he actually likes quite a few books,
he can change the value of the “favorite book” key into an array:
• > db.users.update({"name" : "joe"},
• ... {"$set" : {"favorite book" :
• ... ["Cat's Cradle", "Foundation Trilogy", "Ender's Game"]}})
$unset
• Now if joe realizes that he actually doesn’t like reading, he can remove the key
altogether with "$unset":
• > db.users.update({"name" : "joe"},
• ... {"$unset" : {"favorite book" : 1}})
• Now the document will be the same as it was at the beginning of this example without
the field favorite book
$set to change embedded documents
• You can also use "$set" to reach in and change
embedded documents:
> db.blog.posts.findOne()
> db.blog.posts.findOne()
{
{
"_id" :
"_id" : ObjectId("4b253b067525f35f94b60a31"), ObjectId("4b253b067525f35f94b60a31"),
"title" : "A Blog Post", "title" : "A Blog Post",
"content" : "...", "content" : "...",
"author" : {
"author" : {
"name" : "joe",
"email" : "[email protected]" "name" : "joe schmoe",
} "email" : "[email protected]"

} }

> db.blog.posts.update({"author.name" : "joe"}, }

... {"$set" : {"author.name" : "joe schmoe"}})


Array modifiers
• An extensive class of modifiers exists for manipulating arrays.
• Arrays are common and powerful data structures: not only are they lists that can be
referenced by index, but they can also double as sets.
• Adding elements
• "$push" adds elements to the end of an array if the array exists and creates a new
array if it does not.
• For example, suppose that we are storing blog posts and want to add a
"comments" key containing an array.
• We can push a comment onto the nonexistent "comments" array, which will create the
array and add the comment:
> db.blog.posts.findOne()
db.blog.posts.findOne()
{
"_id" :
{
ObjectId("4b2d75476cc613d5ee930164"),
"_id" : ObjectId("4b2d75476cc613d5ee930164"),
"title" : "A blog post",
"title" : "A blog post",
"content" : "..."
"content" : "...",
}
"comments" : [
> db.blog.posts.update({"title" : "A blog post"},
... {"$push" : {"comments" :
{
... {"name" : "joe", "email" : "name" : "joe",
"[email protected]", "email" : "[email protected]",
... "content" : "nice post."}}}) "content" : "nice post.“ } ]
}
Answer these question with examples
• Can we remove elements from array? Write an example – pop metho
• Can we consider array as sets? Yes or no, justify your answer
• Suppose we have written a query for update and there is no matching
document then what will mongo do in this case? Will it update or discard?
• What helps in inserting a document if it doesn’t exist and update it if it does?
• How safely a write should be stored before the application continues. By
default, inserts, removes, and updates wait for a database response—did the
write succeed or not?—before continuing. Generally, clients will throw an
exception (or whatever the language’s version of an exception is) on failure.
What should you do in this case?
Removing elements
• There are a few ways to remove elements from an array. If you want to treat the
array like a queue or a stack, you can use "$pop", which can remove elements
from either end.
• {"$pop" : {"key" : 1}} removes an element from the end of the array.
• {"$pop" : {"key" : -1}} removes it from the beginning.
• Sometimes an element should be removed based on specific criteria, rather
than its position in the array.
• "$pull" is used to remove elements of an array that match the given criteria.
• For example, suppose we have a list of things that need to be done but not in any specific
order:
• > db.lists.insert({"todo" : [“AAT”, “Lab Programs”, “Reading”]})
• If we do the Reading first, we can remove it from the list with the following:
• > db.lists.update({}, {"$pull" : {“todo”: “reading”}})
• Now apply find()
• We will see that there are only two elements remaining in the array:
• > db.lists.find()
{
"_id" : ObjectId("4b2d75476cc613d5ee930164"),
"todo" : [
“AAT”,
“Lab Programs”
]
}
• Pulling removes all matching documents, not just a single match.
• If you have an array that looks like [1, 1, 2, 1] and pull 1, you’ll end up with a single-element
array, [2].

• Array operators can be used only on keys with array values.


• For example, you cannot push on to an integer or pop off of a string
• Using arrays as sets
• You might want to treat an array as a set, only adding values if they are not
present.
• This can be done using a "$ne" in the query document.
• For example, to push an author onto a list of citations, but only if he isn’t already there, use the
following:
• > db.papers.update({"authors cited" : {"$ne" : "Richie"}},
• ... {$push : {"authors cited" : "Richie"}})
• This can also be done with "$addToSet", which is useful for cases where "$ne" won’t work or
where "$addToSet" describes what is happening better.
• Try examples on $addToSet
Upserts
• An upsert is a special type of update.
• If no document is found that matches the update criteria, a new document will
be created by combining the criteria and updated documents.
• If a matching document is found, it will be updated normally.
• Upserts can be handy because they can eliminate the need to “seed” your
collection: you can often have the same code create and update documents.
• Let’s go back to our example that records the number of views for each page
of a website.
• Without an upsert, we might try to find the URL and increment the number
of views or create a new document if the URL doesn’t exist.
• If we were to write this out as a JavaScript program it might look something
like the following:
// check if we have an entry for this page
blog = db.analytics.findOne({url : "/blog"})
// if we do, add one to the number of views and save
if (blog) {
blog.pageviews++;
db.analytics.save(blog);
}
// otherwise, create a new document for this page
else {
db.analytics.save({url : "/blog", pageviews : 1})
}
Inference
• This means we are making a round trip to the database, plus sending an update
or insert, every time someone visits a page.
• If we are running this code in multiple processes, we are also subject to a race
condition where more than one document can be inserted for a given URL.
• We can eliminate the race condition and cut down on the amount of code by just
sending an upsert (the third parameter to update specifies that this should be an
upsert):
• db.analytics.update({"url" : "/blog"}, {"$inc" : {"pageviews" : 1}},
true)
• This line does exactly what the previous code block does, except it’s faster and
atomic!
• The new document is created using the criteria document as a base and applying
any modifier documents to it.
The save shell helper
• save is a shell function that lets you insert a document if it doesn’t exist and
update it if it does.
• It takes one argument: a document. If the document contains an "_id" key,
save will do an upsert. Otherwise, it will do an insert.
• save is really just a convenience function so that programmers can quickly modify
documents in the shell:
> var x = db.foo.findOne()
> x.num = 42
42
> db.foo.save(x)
• Without save, the last line would have been a more cumbersome
• db.foo.update({"_id" : x._id}, x).
Does Mongo allow multiple updates?
• By default, MongoDB will update only a single document.
• To update multiple documents, you need to set a parameter 'multi' to true.
• >db.mycol.update({'title':'MongoDB Overview'},
• {$set:{'title':'New MongoDB Tutorial'}},{multi:true}) // using
set as modifier
Use of Multiupdates
• Multiupdates are a great way of performing schema migrations or rolling out new
features to certain users.
• Suppose, for example, we want to give a gift to every user who has a birthday on a
certain day.
• We can use multiupdate to add a "gift" to their account:
• > db.users.update({"birthday" : “02/29/2020"},
• ... {"$set" : {"gift" : "Happy Birthday!"}}, false, true)
• This would add the "gift" field to all user documents with birthdays on Feb, 29th
2020
Moving to Querying

You might also like