nodejs 操作mongodb插查删改

var DBUtil=function(mess,name,from){
<span style="white-space:pre">	</span>this.mess=mess;<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>this.from=from;
<span style="white-space:pre">	</span>this.name=name;
}


DBUtil.prototype.setMess=function(mess){
<span style="white-space:pre">	</span>this.mess=mess;
}
DBUtil.prototype.setFrom=function(from){
<span style="white-space:pre">	</span>this.from=from;
}
DBUtil.prototype.setName=function(name){
<span style="white-space:pre">	</span>this.name=name;
}


DBUtil.prototype.insert=function(db,collection){
<span style="white-space:pre">	</span>var data={name:this.name,unread:[{from:this.from,mess:[this.mess]}]};
<span style="white-space:pre">	</span>console.log(data);
<span style="white-space:pre">	</span>db.collection(collection,function(err, collection) {
        collection.insert(data, function(err, docs) {
            if (err) {
                console.log(err);
                return;
            }
        });
    });
}
DBUtil.prototype.update=function(db,collection){
    var name=this.name;
    var from=this.from;
    var mess=this.mess;
    var query={name:name,'unread.from':from};
    var data={'unread.$.mess':mess};
    console.log(this.mess);
<span style="white-space:pre">	</span>db.collection(collection,function(err, collection) {
        collection.update(query,{$push:data},{upsert:true}, function(err, docs) {
            if (err) {
                console.log(err);
                return;
            }
            if(!docs){
                query={name:name};
                data={unread:{from:from,mess:[mess]}};
                console.log(mess);
                collection.update(query,{$push:data},function(err, docs) {
                    if (err) {
                        console.log(err);
                        return;
                    }
                   
                });
            }
        });
    });
}
DBUtil.prototype.getUnRead=function(db,collection,callback){
<span style="white-space:pre">	</span>var query={name:this.name};
<span style="white-space:pre">	</span>db.collection(collection,function(err, collection) {
        collection.find(query).toArray(function(err,docs){
        <span style="white-space:pre">	</span>callback(docs);
        });    
    });
   
}
DBUtil.prototype.remove=function(db,collection,callback){
    var query={name:this.name};
    db.collection(collection,function(err, collection) {
        collection.remove(query,function(error){
            if (err) {
                console.log(err);
                return;
            }
        });
    });
   
}
var dbutil=new DBUtil();
module.exports=dbutil;


对应json格式,_id不包含:

{
    "name": "test",
    "unread": [
        {
            "from": "test1",
            "mess": [
                "这是第一条",
                "这是第二条",
                "这是第三条"
            ]
        },
        {
            "from": "test2",
            "mess": [
                "这是第一条",
                "这是第二条",
                "这是第三条"
            ]
        }
    ]
}

逻辑并不难,难的是语法。。基础比较薄弱

update中,表示不存在则插入

{upsert:true, w: 1}

下载地址:http://download.csdn.net/detail/zbuger/8888039

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值