mongoDB update used for handle old data.

1. be resilient to data changes

> db.books.update({},{$set:{“keywords”:[]}},false,true);

 

2. Updating part of a document

> db.books.update( { author: "Kristina Chodorow" },
{ "$push": { "keywords": "developer" } } );
> db.books.find();
{ "_id" : ObjectId("5063d1d89e302eaf24b259a0"), "author" : "Jamie Munro",

  "title" : "20 Recipes for Programming PhoneGap",
"published" : ISODate("2012-04-03T07:00:00Z") }
{ "_id" : ObjectId("5063d6909e302eaf24b259a1"), "author" : "Kristina Chodorow",

  "keywords" : [ "design", "implementation", "optimization", "developer" ],
  "published" : ISODate("2011-05-06T07:00:00Z"),
  "title" : "50 Tips and Tricks for MongoDB Developers" }

3. SELECT * FROM books;
Example 4-5. A simple field search in MongoDB

> db.books.find({author: "Jamie Munro"});

Example 4-6. Finding specific fields from a collection

> db.books.find({}, {title:1});
{ "_id" : ObjectId("5063d6909e302eaf24b259a1"),

  "title" : "50 Tips and Tricks for MongoDB Developers" }
{ "_id" : ObjectId("5063d1d89e302eaf24b259a0"),

"title" : "20 Recipes for Programming PhoneGap" }

Example 4-7. Diagnosing slow queries

> db.books.find({author: "Jamie Munro"}).explain();
{

“cursor” : “BasicCursor”, “nscanned” : 2, “nscannedObjects” : 2, “n”:1,

  "millis" : 0,
  "nYields" : 0,
  "nChunkSkips" : 0,
  "isMultiKey" : false,
  "indexOnly" : false,
  "indexBounds" : {

} }

Example 4-8. Adding an index to a collection

> db.books.ensureIndex({author:1});
> db.books.find({author: "Jamie Munro"}).explain();
{

“cursor” : “BtreeCursor author_1”, “nscanned” : 1,
“nscannedObjects” : 1,
“n”:1,

  "millis" : 0,
  "nYields" : 0,
  "nChunkSkips" : 0,
  "isMultiKey" : false,
  "indexOnly" : false,
  "indexBounds" : {
  "author" : [
[
      "Jamie Munro",
      "Jamie Munro"

] ]

} }

Example 4-9. Performing a MapReduce query on the books collection

> db.books.insert({"author": "Kristina Chodorow", "title": "Scaling MongoDB",
... "published": new Date("03/02/2011")});
> db.books.insert({"author": "Stoyan Stefanov", "title": "JavaScript Patterns",
... "published": new Date("09/28/2010")});

> db.books.insert({"author": "Stoyan Stefanov",
... "title": "JavaScript for PHP Developers",
... "published": new Date("10/22/2012")});
> db.books.insert({"author": "Stoyan Stefanov", "title": "Web Performance Daybook",
... "published": new Date("06/27/2012")});

> db.books.insert({"author": "Jamie Munro",
... "title": "20 Recipes for Programming MVC 3",
... "published": new Date("10/11/2011")});

> map = function() { emit( this.author, 1 ); };
function () {

    emit(this.author, 1);
}

> reduce = function( key, values ) {

… var total=0;
… values.forEach(function(value){ … total+=value;
… });
… return total;
… }
function (key, values) {

    var total = 0;
    values.forEach(function (value) {total += value;});
    return total;

}
> db.books.mapReduce(map,reduce, { out: "bookoutput"});
{

  "result" : "bookoutput",
  "timeMillis" : 3,
  "counts" : {

    "input" : 7,
    "emit" : 7,
    "reduce" : 3,
    "output" : 3

},

“ok” : 1, }

> db.bookoutput.find();
{ "_id" : "Jamie Munro", "value" : 2 }
{ "_id" : "Kristina Chodorow", "value" : 2 }
{ "_id" : "Stoyan Stefanov", "value" : 3 }


Concurrent Access 

Updating documents using findAndModify

 

db.articles.save( {
… title: “Jolly Roger”,
… published: “September 12, 2007”,
… description: “A riveting tale of suspense and drama.”, … revision: “a5”
…});

> db.articles.find();

{
  "_id" : ObjectId("505a9b4fd5f42989fe6d8015"),
  "title" : "Jolly Roger",
  "published" : "September 12, 2007",
  "description" : "A riveting tale of suspense and drama.",
  "revision" : "a5"

}

> db.articles.findAndModify({
... query: {"_id": ObjectId("505a9b4fd5f42989fe6d8015"), revision: "a5"},
... update: {$set: {revision: "b7"} }
... });
{

  "_id" : ObjectId("505a9b4fd5f42989fe6d8015"),
  "title" : "Jolly Roger",
  "published" : "September 12, 2007",
  "description" : "A riveting tale of suspense and drama.",
  "revision" : "a5"

}

> db.articles.find();
{

  "_id" : ObjectId("505a9b4fd5f42989fe6d8015"),
  "title" : "Jolly Roger",
  "published" : "September 12, 2007",
  "description" :

  "A riveting tale of suspense and drama.",

  "revision" : "b7"
}

> db.articles.findAndModify({
... query: {"_id": ObjectId("505a9b4fd5f42989fe6d8015"), revision: "a5"},
... update: {$set: {revision: "90jasv"} }
... });
null

> db.articles.find();
{

  "_id" : ObjectId("505a9b4fd5f42989fe6d8015"),
  "title" : "Jolly Roger",
  "published" : "September 12, 2007",
  "description" : "A riveting tale of suspense and drama.",
  "revision" : "b7"

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s