MongoDB: Working with JS ( For Loop)

MongoDB itself has provided some basic looping mechanism through which you can achieve iteration functionality.
let’s discuss one by one.

FOR LOOP:- For loop is a basic loop available in mongodb and its syntax is just same as we have in other technologies.

for(var i=0;i<100;i++){
–list of statements;
.;
..;
…;
..n;
}
Within the curly braces{..} you can write your own statements and each of them will be executed till the loop condition satisfies.

lets see one examples using FOR LOOP

******************************************************************************
BASIC INSERTS AND DELETES
******************************************************************************
For(var i=0;i<100;i++){
db.things.insert({ids:i}); –to insert a document in a collection things.
db.things.remove({ids:i}); –to remove a document from a collection things.
print (i); –to print a local variable value i.
}
*******************************************************************************

IF(CONDITION):-
MongoDB also provides you with the functionality of checking a condition and then performs some operation.
here is one example using FOR LOOP INTEGRATED WITH IF CONDITION

******************************************************************************
REMOVING DOCUMENTS CONTAINING EVEN ids FROM COLLECTION
******************************************************************************

For(var i=0;i<100;i++){ if(i%2==0) –checking whether a condition is true or not { db.things.remove({ids:i}); –to remove a document from a collection things. print (i); –to print a local variable value i. } } *This loop will removes all the documents which have even ids. ******************************************************************************* CURSOR ATTRIBUTES When you fetch a result and store it in a variable then you have certain attributes by which you can iterate over the retrieved data and perform several actions. cursor.hasNext()–> It returns Boolean either the true or false i.e. whether after the current document any other document is present or not.
cursor.next()–> It will return you the present value and move the cursor pointer to the next available document.
Lets see a basic example…

*******************************************************************************
WHILE LOOP WITH CURSOR ATTRIBUTES
*******************************************************************************

var curTemp = db.things.find(); –declaring a variable curTemp and initializing it with query result.
var temp;
while(curTemp.hasNext()){
temp=curTemp.next(); –returning a json document and assigning it to temp variable
printjson (temp); — it will print json document one by one
print (temp.ids); –it will print the values of ids field as accessed by . operator.
}
*Once the cursor is completely traversed then it will not return any value because the other attribute curTemp.hasNext() return false.
error:-uncaught exception: error hasNext: false

*******************************************************************************
USING FOREACH:- Foreach provides you better functionality, using foreach you do not have to use cursor attributes such as curosr.hasNext() and cursor.next(). It implicitly performs those operations it automatically iterates on the data/documents available.
syntax:-

db.collection_name.find().forEach( function(obj)
{
.
..

}
);

let’s go through an example:-The problem defines that i want to insert a column that will contain employee name,department_no and empno in a concatenated format.

*******************************************************************************
USING FOREACH
*******************************************************************************

db.employee.find().forEach( function(obj)
{
empno_str = (obj.empno).toString()+”-“;
deptno_str = (obj.deptno).toString();
name_str = obj.name+”-“;
final = empno_str+name_str+deptno_str;
obj.new_field_name = final;
print (final);
db.employee.save(obj);
}
);

*using obj. you can access any field of the selected document and in this operator save function is updating the selected field as _id field is already present.more info UPDATE USING SAVE

  • Ask Question