MongoDB ObjectId

Even though we have been mentioning about MongoDB ObjectId  in all the previous section, we didn’t look well into what constitute a MongoDB ObjectId.  MongoDB ObjectId is an ObjectId and is a 12-byte BSON   having the accompanying structure:

  • The initial 4 bytes are the  seconds since the Unix age
  • The following 3 bytes are the machine identifier
  • The following 2 bytes comprises of procedure id
  • The last 3 bytes are an irregular counter esteem

 

MongoDB- ObjectId
MongoDB- ObjectId

 

MongoDB utilizes Objectids as the default value of _id field of each one document which is produced while making of any record. The complex synthesis of ObjectId makes all the _id fields interesting.

Creation of New ObjectId

Use the below code to produce another ObjectId

newObjectId = ObjectId()

The above articulation furnishes a proportional payback extraordinarily produced id:

ObjectId("6249b4ddd2781d08c09890d2")

Rather than MongoDB producing the ObjectId, you can likewise give a 12-byte id:

myObjectId = ObjectId("6249b4ddd2781d08c09890d2")

 Creation Timestamp of a Document

Since the _id ObjectId naturally stores the 4 byte so there is no need to store the creation time of any document. You can get the creation time of a record utilizing gettimestamp system:

objectId("6249b4ddd2781d08c09890d2").gettimestamp()

Report in ISO Date form –

ISODate("2014-04-12t21:49:17Z")<strong> </strong>

Change over ObjectId to String

At times you may require the ObjectId in string format. To change over the ObjectId in string , utilize the accompanying code:

newobjectId("6249b4ddd2781d08c09890d2").str

The above code will furnish :

6249b4ddd2781d08c09890d2

MongoDB MapReduce

According to the MongoDB documentation, MapReduce is  an information handling standard for consolidating vast volumes of information into valuable collected results. MongoDB utilizes mapReduce command to lessens operations. Mapreduce is by and large utilized for transforming substantial information sets.

MapReduce Command:

Lets look into the basic structure of a  mapReduce command:

db.collection.mapReduce(capacity() {emit(key,value);},
function(key,values) {return reduceFunction}
{
out: collection,
query: document,
sort: docuent,
limit: number
}
)

The code first inquiries the gathering, then maps the result documents to radiate key-quality sets which is then focused around the keys that have different qualities.

In the above syntax:

  • map is a javascript work that maps a value with a key and transmits a key-value pair
  • reduce is a javscript work that decreases or gathers all the documents having the same key
  • out specifies the area of the map-reduce inquiry result
  • query specifies the discretionary choice criteria for selecting reports
  • sort specifies the discretionary sort criteria
  • limit specifies the discretionary most extreme number of reports to be returned

Utilizing Mapreduce:

Consider the accompanying record structure putting away client posts. The document stores user_name of the client and the status of post.

{"post_text": "J2EEbrain.com is an great site for Learning",
"user_name": "Parker",
"status":"active"
}

Presently, we will utilize a mapReduce function  on the  posts  collection  and  select all the dynamic posts, bunch them on the premise of username and afterward tally the quantity of posts by every client utilizing the accompanying code:

db.posts.mapReduce(function() { emit(this.user_id,1); },

function(key, values)
{
query:{status:"active"},
out:"post_total"
}
)

The above mapreduce inquiry yields the accompanying result:

{"result" : "post_total",
"timeMillis" : 9,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 2,
"output" : 2
},
"alright" : 1,
}

The result demonstrates that an aggregate of 4 document matched the inquiry (status:”active”), the map function gave 4 records with key-value pairs and lastly the reduce function assembled mapped reports having the same keys into 2.

To see the after effect of this code utilize the find() function:

db.posts.mapReduce(function() { emit(this.user_id,1); },

function(key, values)

{
query:{status:"active"},
out:"post_total"
}
).find()