时间:2021-07-01 10:21:17 帮助过:26人阅读
let mongoClient = require("MonGoDB").MongoClient;
let url = "mongodb://localhost:27017/admin";
mongoClient.connect(url, function (err, db) {
if (err) {
console.log("数据库连接失败!");
} else {
insertOne(db);
}
});
function insertOne(db) {
db.collection("User").insertOne({
"name": "测试",
"sex": 1
}, function (err, result) {
console.log(result.result);
console.log(result.ops)
db.close();
});
}
let MongoClient = require("MonGoDB").MongoClient;
let ObjectId = require("MonGoDB").ObjectID;
let url = "mongodb://localhost:27017/admin";
MongoClient.connect(url, function (err, db) {
if (err) {
console.log("数据库连接失败!");
} else {
deleteData(db);
}
});
function deleteData(db) {
db.collection("User").deleteOne({
_id: new ObjectId("59ba30b2d5f3741b9831bea8")
}, function (err, result) {
console.log(result.result);
db.close();
});
}
let mongoClient = require("MonGoDB").MongoClient;
let url = "mongodb://localhost:27017/admin";
mongoClient.connect(url, function (err, db) {
if (err) {
console.log("数据库连接失败!");
} else {
uodateData(db);
}
});
function uodateData(db) {
var cursor = db.collection("User").updateMany({
"name": "张三"
}, {
"$set": {
"name": "陈辉",
"sex": 1
}
}, function (err, result) {
console.log(result.result);
});
}
let mongoClient = require("MonGoDB").MongoClient;
let ObjectId = require("MonGoDB").ObjectID;
let url = "mongodb://localhost:27017/admin";
mongoClient.connect(url, function (err, db) {
if (err) {
console.log("数据库连接失败!");
} else {
selectAll(db);
}
});
function selectAll(db) {
// 精确查找
// var cursor = db.collection("User").find({_id: new ObjectId("59ba30970692cd172083a836")});
// 查询全部
var cursor = db.collection("User").find();
var result = [];
cursor.each(function (err, doc) {
if (doc == null) {
console.log(result);
db.close();
} else {
result.push(doc);
}
})
}
MonGoDb 简单的增删改查
标签:结合 remove 目录 use 字段 rto 常用 err 查询条件