Sunday, June 11, 2017

23/05/2017

                                           MONGODB  NODEJS



SORT

Use the sort() method to sort the result in ascending or descending order.

Use the value -1 in the sort object to sort descending.
        { name: 1 } // ascending
        { name: -1 } // descending


var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/uki";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var mysort ={name:1};  db.collection("studentsdetails").find().sort(mysort).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});





REMOVE


Use the remove() method to delete records, or documents.

var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/uki";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var myquery = { address: 'Colombo' };  db.collection("studentsdetails").remove(myquery, function(err, obj) {
    if (err) throw err;
    console.log(obj.result.n + " document(s) deleted");
    db.close();
  });
});


No comments:

Post a Comment

HTML introduction

HTML stands for Hyper Text Markup Language. HTML describes the structure of web page using markup. HTML elements are the building blo...