连接数据库
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(