MongoDB Archive

Insert a document using NodeJs and mongoDB

Insert a document using Node.js and mongoDB Suppose  you have a mongoDB database for courses and you want to insert a record into the database using Node.js. First step is to install mongoDB driver for Node.js  If it is not already installed. Please open command line and run following command. > npm install mongodb This

Node Js, MongoDb select record with findOne

Node Js, MongoDb select record with findOne To select a single record from mongoDB database using  findOne() function  in Node Js var MongoClient = require('mongodb').MongoClient; //Open the connection to server MongoClient.connect('mongodb://localhost:27017/test', function(err, db){ if(err) throw err; db.collection('people').findOne({}, function(err, doc){ if(err) throw err; console.log(doc); db.close(); }); console.dir("Called findOne"); }); In the code below we use require

$unset operator in mongoDB

$ unset operator in mogoDB In this article were going to explore how $ unset operator in mongodb is used to remove a field from a mongodb document. Syntax: { $unset: { <field1>: "", ... } }   $unset operator is used to delete a field from a collection. Consider a collection named “users”.  Suppose

NodeJS – select data from mongoDB collection

Suppose we have a collection in MongoDB database, We want to select data from collection then we can do like the program below. Suppose we have a collection ( Like table in Relational DBMS)  named grades and this collection is in course database.   Grades collection contains fields like _id, student, grade and so on…  now

mongoDB – Index Size

We can determine index size of a collection by using following command in mongoDB. Suppose our collection name is “students”. > db.students.totalIndexSize();   Our index and data both can take space in memory but it is more important that our index fit into the memory.

mongoDB – import a CSV file in a database collection

Suppose if you have a CSV file with large number of records and you want to import it into  database collection then need to use mongoimport mongoimport --db users --collection contacts --type csv --headerline --file contacts.csv --db will specify database, --collection will represent contacts collection --type is representing that  CSV file --headerline is representing the

mongoDB – remove a document

In mongoDB we can remove a document like in SQL. In mongoDb we use remove function to remove a document. In remove function we specify some criteria that is like where clause in SQL if we want to remove a document where name is mike the we will do it like this. > db.users.remove({"name":"mike"});  

mongoDB – Multi update of documents

In mongoDB an update operation can be used in 4 ways 1. wholesale replacement of a document. 2. update individual field using $set operator 3. upsert  – do update or insert 4. multiupdate of a document We have already read about first 3 let us look at multiupdate of a document. if we run following

mongoDB – Upserts – insert or update a document

If we use update command the document is updated only if that document record is present in collection. But if the requirement is that if document is present then we update it otherwise insert it as a new record. Then we can use upsert command. Suppose we run an update command as follows  > db.users.update({"name":"mike"},{$set:{$age:

mongoDB – Update a document in database

As in SQL We use update command for updating a record,  We do the same in No SQL databases. DB. We use update command. This approach is known as wholesale update of a document. Suppose we have a document in users collection. >  { _id :1, "name": "Mike"} Following is Syntax of update. > db.collection.update(query,