NodeJs and Express framework basics We have already learned how to select data from MongoDb using Nodejs findOne. In this article we will create a baisc Hello World program using NodeJs and Express. Consider a following program below. [crayon-5dd1f018d58b2086410907/] In our first line we need to include ‘express’ module. If express is not already installed
Node Js, MongoDb select record with findOne To select a single record from mongoDB database using findOne() function in Node Js [crayon-5dd1f018d8944281482399/] In the code below we use require function to include the module ‘mongodb’.MongoClient and is assigned to a variable MongoClient [crayon-5dd1f018d8956562557980/] After including mongodb module we have to connect to the server. Consider
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
We can determine index size of a collection by using following command in mongoDB. Suppose our collection name is “students”. [crayon-5dd1f018e5483401357949/] Our index and data both can take space in memory but it is more important that our index fit into the memory.
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 [crayon-5dd1f018e5fe8732094739/] [crayon-5dd1f018e5ff7739320357/] [crayon-5dd1f018e6002940486262/] will specify database, [crayon-5dd1f018e600c583827641/] will represent contacts collection [crayon-5dd1f018e6017155141704/] is representing that CSV file [crayon-5dd1f018e6021986024403/] is representing the top line in file is header or column names.
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. [crayon-5dd1f018e6d8d358551221/] If
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
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 [crayon-5dd1f018e8629765481379/] If
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. [crayon-5dd1f018e91ad221654890/] Following is Syntax of update. > db.collection.update(query, update, options) Query is like
The $all operator is used to selects the documents with the value of field is an array that contains all given elements. where the value of a field is an array that contains all the specified elements. This operator behaves like $and operator. $all syntax is [crayon-5dd1f018e9d4a161051144/] [crayon-5dd1f018e9d59350400380/] This query will return all documents with