node-mongoDB
生活随笔
收集整理的這篇文章主要介紹了
node-mongoDB
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
連接數據庫
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {if (err) throw err;console.log("數據庫已鏈接!");db.close();
});創建集合
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017';
MongoClient.connect(url, { useNewUrlParser: true }, function (err, db) {if (err) throw err;console.log('數據庫已連接');var dbase = db.db("runoob");dbase.createCollection('site', function (err, res) {if (err) throw err;console.log("創建集合!");db.close();});
});數據插入(如果集合不存在,會自動創建集合)
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";MongoClient.connect(url, { useNewUrlParser: true }, function (err, db) {if (err) throw err;console.log("數據庫連接成功");var dbo = db.db("runoob");var myobj = { name: "baidu", url: "www.baidu.com" };dbo.collection("web").insertOne(myobj, function (err, res) {if (err) throw err;console.log("數據插入成功");db.close();});
});數據插入(多條)
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";MongoClient.connect(url, { useNewUrlParser: true }, function (err, db) {if (err) throw err;console.log("數據庫連接成功");var dbo = db.db("runoob");var myobj = [{ name: "baidu", url: "www.baidu.com" },{ name: "google", url: "www.google.com" },{ name: "bbb", url: "www.bilibili.com" }
];dbo.collection("web").insertMany(myobj, function (err, res) {if (err) throw err;console.log("數據插入成功"+res.insertedCount);db.close();});
});查詢
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {if (err) throw err;var dbo = db.db("runoob");var whereStr = {"name":'baidu'}; // 查詢條件dbo.collection("web").find(whereStr).toArray(function(err, result) {if (err) throw err;console.log(result);db.close();});
});更新一條數據
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {if (err) throw err;var dbo = db.db("runoob");var whereStr = {"name":'baidu'}; // 查詢條件var updateStr = {$set: { "url" : "https://www.baiduxx.com" }};dbo.collection("web").updateOne(whereStr, updateStr, function(err, res) {if (err) throw err;console.log("數據更新成功");db.close();});
});更新多條數據
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {if (err) throw err;var dbo = db.db("runoob");var whereStr = {"url":'www.baidu.com'}; // 查詢條件var updateStr = {$set: { "url" : "www.baidux2.com" }};dbo.collection("web").updateMany(whereStr, updateStr, function(err, res) {if (err) throw err;console.log(res.result.nModified + " 條數據被更新");db.close();});
});輸出一條數據
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {if (err) throw err;var dbo = db.db("runoob");var whereStr = {"name":'bizhan'}; // 查詢條件dbo.collection("web").deleteOne(whereStr, function(err, obj) {if (err) throw err;console.log("數據刪除成功");db.close();});
});刪除多條數據
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {if (err) throw err;var dbo = db.db("runoob");var whereStr = {"name":'baidu'}; // 查詢條件dbo.collection("web").deleteMany(whereStr, function(err, obj) {if (err) throw err;console.log("數據刪除成功");db.close();});
});排序
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {if (err) throw err;var dbo = db.db("runoob");var mysort = { url: 1 };dbo.collection("web").find().sort(mysort).toArray(function(err, result) {if (err) throw err;console.log(result);db.close();});
});左聯合集合1:order[{ _id: 1, product_id: 154, status: 1 }
]
集合2:products[{ _id: 154, name: '筆記本電腦' },{ _id: 155, name: '耳機' },{ _id: 156, name: '臺式電腦' }
]代碼
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/";MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {if (err) throw err;var dbo = db.db("runoob");dbo.collection('order').aggregate([{ $lookup:{from: 'products', // 右集合localField: 'product_id', // 左集合 join 字段foreignField: '_id', // 右集合 join 字段as: 'orderdetails' // 新生成字段(類型array)}}]).toArray(function(err, res) {if (err) throw err;console.log(JSON.stringify(res));db.close();});
});執行結果
[{"_id":1,"product_id":154,"status":1,"orderdetails":[{"_id":154,"name":"手機"}]}]其他很多(跟mongoDB的正常語法都是一樣的)
mongoDB筆記: https://blog.csdn.net/u013761036/article/details/101054229指定了返回的條數
dbo.collection("site").find().limit(2).toArray(
如果要指定跳過的條數
dbo.collection("site").find().skip(2).limit(2).toArray(
刪除集合
dbo.collection("test").drop(
?
總結
以上是生活随笔為你收集整理的node-mongoDB的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: node-多进程
- 下一篇: node-mongo封装