Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

connecting , creating ,reading from mongo

const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mytestingdb';

const retrieveCustomers = (db, callback)=>{
    // Get the customers collection
    const collection = db.collection('customers');
    // Find some customers
    collection.find({}).toArray((err, customers) =>{
        if(err) throw err;
      console.log("Found the following records");
      console.log(customers)
      callback(customers);
    });
}

const retrieveCustomer = (db, callback)=>{
    // Get the customers collection
    const collection = db.collection('customers');
    // Find some customers
    collection.find({'name': 'mahendra'}).toArray((err, customers) =>{
        if(err) throw err;
      console.log("Found the following records");
      console.log(customers)
      callback(customers);
    });
}

const insertCustomers = (db, callback)=> {
    // Get the customers collection
    const collection = db.collection('customers');
    const dataArray = [{name : 'mahendra'}, {name :'divit'}, {name : 'aryan'} ];
    // Insert some customers
    collection.insertMany(dataArray, (err, result)=> {
        if(err) throw err;
        console.log("Inserted 3 customers into the collection");
        callback(result);
    });
}

// Use connect method to connect to the server
MongoClient.connect(url,{ useUnifiedTopology: true }, (err, client) => {
  console.log("Connected successfully to server");
  const db = client.db(dbName);
  insertCustomers(db, ()=> {
    retrieveCustomers(db, ()=> {
        retrieveCustomer(db, ()=> {
            client.close();
        });
    });
  });
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: replace espacial characteres from string 
Javascript :: avoid-browser-pop-up-blockers 
Javascript :: how to use javascript so the color box change color and then change back 
Javascript :: changing state in useeffect...Avoid rerender because of useeffect....Call useEffect only once....Infinite loop useEffect 
Javascript :: always shouldComponentUpdate return false to make program fast and performant 
Javascript :: scope hierarchy in angularjs 
Javascript :: How to update Code Mirror data on modal show 
Javascript :: how to redirect from login page to other page if user is already logged in in angular using jwt 
Javascript :: how to decrease the size of js files build from angular 
Javascript :: %20find%20all%20docs%20that%20have%20at%20least%20two%20name%20array%20elements_ 
Javascript :: number and type operators in javascript 
Javascript :: javascript formdata include object 
Javascript :: Stateful/Container/Smart component 
Javascript :: close responsive menu after click 
Javascript :: delete all properties from an javascript object second solution 
Javascript :: modulos nodejs 
Javascript :: Calling JSON REST Services with FoxPro and wwJsonServiceClient 
Javascript :: ES6 template literals sum example 
Javascript :: diagonal difference javascript 
Javascript :: force reload when back_forward is clicked 
Javascript :: return where an property eqauls 
Javascript :: find short number in array javascript 
Javascript :: add if condition in map react 
Javascript :: how to get all key values of Json 
Javascript :: find document which is not in array 
Javascript :: mapa gratis leaflet 
Javascript :: web3 js connect to ropsten 
Javascript :: online regex generator based on string 
Javascript :: argument and parameter 
Javascript :: react foreach loop 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =