// 连接数据库取代了db.open(...)
mongodbClient.connect(url, function(err, db) {
if(err) {
return callback(err);
}
var collection = db.collection('posts');
collection.insertOne(post, function(err) {
if(err) {
return callback(err);
}
callback(null);
db.close();
})
})通过collection操作数据库的方法。我打印了collection.__proto__,如下所示:{ collectionName: [Getter],
namespace: [Getter],
readConcern: [Getter],
writeConcern: [Getter],
hint: [Getter/Setter],
find: [Function],
insertOne: [Function],
insertMany: [Function],
bulkWrite: [Function],
insert: [Function],
updateOne: [Function],
replaceOne: [Function],
updateMany: [Function],
update: [Function],
deleteOne: [Function],
removeOne: [Function],
deleteMany: [Function],
removeMany: [Function],
remove: [Function],
save: [Function],
findOne: [Function],
rename: [Function],
drop: [Function],
options: [Function],
isCapped: [Function],
createIndex: [Function],
createIndexes: [Function],
dropIndex: [Function],
dropIndexes: [Function],
dropAllIndexes: [Function],
reIndex: [Function],
listIndexes: [Function],
ensureIndex: [Function],
indexExists: [Function],
indexInformation: [Function],
count: [Function],
distinct: [Function],
indexes: [Function],
stats: [Function],
findOneAndDelete: [Function],
findOneAndReplace: [Function],
findOneAndUpdate: [Function],
findAndModify: [Function],
findAndRemove: [Function],
aggregate: [Function],
parallelCollectionScan: [Function],
geoNear: [Function],
geoHaystackSearch: [Function],
group: [Function],
mapReduce: [Function],
initializeUnorderedBulkOp: [Function],
initializeOrderedBulkOp: [Function] }2、mongodb更新一个数组,如commets是一个数组,添加commentcollection.update({
"name": name,
}, {
$push: {"comments": comment}
})3、distinct(key, query, options, callback){Promise}查询集合返回一个带有key键的值组成的列表,列表的值是不重复的。其中每个集合需要满足query条件。var MongoClient = require('mongodb').MongoClient,
test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
// 创建集合
var collection = db.collection('distinctExample2');
// 插入多个文档
collection.insertMany([{a:0, b:{c:'a'}}, {a:1, b:{c:'b'}}, {a:1, b:{c:'c'}},
{a:2, b:{c:'a'}}, {a:3}, {a:3}, {a:5, c:1}], {w:1}, function(err, ids) {
// 返回含有c是1的集合的a属性的值组成的列表。 [5]
collection.distinct('a', {c:1}, function(err, docs) {
test.deepEqual([5], docs.sort());
db.close();
});
})
});
collection.updateOne({
"name": name,
"time.day": day,
"title": title
}, {
$inc: {"pv": 1}
}, function(err) {
if(err) {
return callback(err);
}
db.close();
})6、mongodb分页,主要用到count和skip和limit属性Post.getTen = function(name, page, callback) {
mongodbClient.connect(url, function(err, db) {
if(err) {
return callback(err)
}
var collection = db.collection('posts');
var query = {};
if(name) {
query.name = name;
}
// 对于一个query对象,首先使用`count()`来查询到总数目,结果值赋给参数`total`,比如`total`是93,然后使用`find()`查询,并跳过(page-1)*10个结果,返回之后的10个结果,按时间(`time`)降序排序。1是升序,也是默认的。-1是降序
collection.count(query, function(err, total) {
collection.find(query).skip((page - 1) * 10).limit(10).sort({
time: -1
}).toArray(function(err, docs) {
if(err) {
return callback(err);
}
callback(null, docs, total);
db.close();
})
})
})
}