node.js操作mongoDB数据库示例分享

2020-06-17 06:06:10易采站长站整理

var db=new mongo.Db(“node-mongo-examples”,server,{safe:true});
var docs=[
    {type:”food”,price:11},
    {type:”food”,price:10},
    {type:”food”,price:9},
    {type:”food”,price:8},
    {type:”book”,price:9}
];
db.open(function (err,db) {
    db.collection(“goods”, function (err,collection) {
        if(err) throw err;
        else{
            collection.insert(docs, function (err,docs) {
                if(err) throw  err;
                else{
                    collection.find({type:”food”,price:{$lt:10}}).toArray(
                        function(err,docs){
                            if(err) throw err;
                            else{
                                console.log(docs);
                                db.close();
                            }
                        }
                    );
                }
            })
        }
    });
});

查询中的或的表达:


collection.find({$or:[
    {type:”food”},
    {price:{$lt:10}}
  ]})

有关node.js操作mongoDB数据库的讲解,今天就先到这里了,基本上常用的操作都有了示例,复杂些的,小伙伴们自由发挥吧,有机会我们再来继续讲解。