let MongoClient = require("mongodb").MongoClient;
let url = "mongodb://192.168.200.100:27017/admin"; MongoClient.connect(url, function (err, db) {
if (err) {
console.log("数据库连接失败!");
} else {
/* 验证账号密码, 每个库都会有自己的账号密码, 超级用户也不能访问所有的 */
db.authenticate("root", "920619", function () {
insertOne(db);
});
}
}); function insertOne(db) {
db.collection("User").insertOne({
"name": "测试二",
"sex": 0
}, 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://192.168.200.100:27017/admin"; MongoClient.connect(url, function (err, db) {
if (err) {
console.log("数据库连接失败!");
} else {
/* 验证账号密码, 每个库都会有自己的账号密码, 超级用户也不能访问所有的 */
db.authenticate("root", "920619", function () {
deleteData(db);
});
}
}); function deleteData(db) {
db.collection("User").deleteOne({
_id: new ObjectId("59ba30e093f3501f8c8c468e")
}, function (err, result) {
console.log(result.result);
db.close();
});
}
let MongoClient = require("mongodb").MongoClient;
let url = "mongodb://192.168.200.100:27017/admin"; MongoClient.connect(url, function (err, db) {
if (err) {
console.log("数据库连接失败!");
} else {
/* 验证账号密码, 每个库都会有自己的账号密码, 超级用户也不能访问所有的 */
db.authenticate("root", "920619", function () {
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://192.168.200.100:27017/admin"; MongoClient.connect(url, function (err, db) {
if (err) {
console.log("数据库连接失败!");
} else {
/* 验证账号密码, 每个库都会有自己的账号密码, 超级用户也不能访问所有的 */
db.authenticate("root", "920619", function () {
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);
}
});
}