Apply for Zend Framework Certification Training

MongoDb






Sort() is the not a method hence it can not be used standalone so we, 
in most of the cases, use it along with find() method of mongodb.
It is used to sort the collection in either ascending of descending order.


//condition "1" for sorting in Ascending order on the basis of "age"	
	var method = { age : 1 };
	
	//accessing the collection
	db.collection("new")
		.find()
		.sort(method)
		.toArray( (err , collection) => {
		if(err) throw err;
		//console.log(collection.result.n + "Record(s) deleted successfully");
		console.log(collection);
		db.close();
	});
	
	
	//escending order on the basis of "name"	
	var method = { name : -1 };


< Uses of updateOne and updateMany in mongoDb Uses of $pull in mongoDb with nodeJs >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top