1//CRUD
2
3const mongodb = require('mongodb');
4const MongoClient = mongodb.MongoClient;
5
6const connectionURl = 'mongodb://127.0.0.1:27017';
7const databaseName = 'task-manager';
8
9MongoClient.connect(
10 connectionURl,
11 { useNewUrlParser: true },
12 (error, client) => {
13 if (error) {
14 return console.log('unable to connect to database');
15 }
16
17 const db = client.db(databaseName);
18
19 db.collection('users').insertOne({
20 name: 'user',
21 age: 24,
22 });
23 }
24);
25
1const { MongoClient } = require("mongodb");
2// Replace the uri string with your MongoDB deployment's connection string.
3const uri =
4 "mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
5const client = new MongoClient(uri);
6async function run() {
7 try {
8 await client.connect();
9 const database = client.db("sample_mflix");
10 const movies = database.collection("movies");
11 // create an array of documents to insert
12 const docs = [
13 { name: "Red", town: "Kanto" },
14 { name: "Blue", town: "Kanto" },
15 { name: "Leon", town: "Galar" }
16 ];
17 // this option prevents additional documents from being inserted if one fails
18 const options = { ordered: true };
19 const result = await movies.insertMany(docs, options);
20 console.log(`${result.insertedCount} documents were inserted`);
21 } finally {
22 await client.close();
23 }
24}
25run().catch(console.dir);
1var MongoClient = require('mongodb').MongoClient;
2var url = 'mongodb://localhost:27017/test';
3var data1={
4 name:'Data1',
5 work:'student',
6 No:4355453,
7 Date_of_birth:new Date(1996,10,17)
8};
9
10var data2={
11 name:'Data2',
12 work:'student',
13 No:4355453,
14 Date_of_birth:new Date(1996,10,17)
15};
16
17MongoClient.connect(url, function(err, db) {
18 if(err!=null){
19 return console.log(err.message)
20 }
21
22 //insertOne
23 db.collection("App").insertOne(data1,function (err,data) {
24
25 if(err!=null){
26 return console.log(err);
27 }
28 console.log(data.ops[0]);
29 });
30
31 //insertMany
32
33var Data=[data1,data2];
34
35db.collection("App").insertMany(Data,forceServerObjectId=true,function (err,data) {
36
37 if(err!=null){
38 return console.log(err);
39 }
40 console.log(data.ops);
41 });
42 db.close();
43});