怎么nodejs中mongodb查询完后再添加
发布网友
发布时间:2022-04-25 10:09
我来回答
共1个回答
热心网友
时间:2022-04-07 21:58
查询
var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
var db = new mongodb.Db('mydb',server,{safe:true});
db.open(function(err,db){
if(!err)
{
db.collection('mycoll',{safe:true},function(err,collection){
var tmp1 = {title:'hello'};
var tmp2 = {title:'world'};
collection.insert([tmp1,tmp2],{safe:true},function(err,result){
console.log(result);
});
collection.find().toArray(function(err,docs){
console.log('find');
console.log(docs);
});
collection.findOne(function(err,doc){
console.log('findOne');
console.log(doc);
});
});
删除
var mongodb = require('mongodb');
var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
var db = new mongodb.Db('mydb',server,{safe:true});
db.open(function(err,db){
if(!err)
{
db.collection('mycoll',{safe:true},function(err,collection){
collection.remove({title:'hello'},{safe:true},function(err,result){
console.log(result);
});
}else{
console.log(err);
}
});