淘先锋技术网

首页 1 2 3 4 5 6 7

初始化:

show dbs
db
use wx_game
db.dropDatabase();

创建数据:

var userNum = 10;

for (var i = 1; i <= userNum; i++) {
   db['user'].insert({
       "name": "zhangsan" + i,
       "sex": ((Math.round(Math.random() * 10) % 2)===0 ? 'M' : 'F'),
       "age": Math.round(Math.random() * 6) + 3,
       "hobby": []
   });
}

查询数据:

db.getCollection('user').find({age: {$exists: true}});
db.getCollection('user').find({loan:{$exists: true},"loan.bankLoan":NaN}).sort({_id:-1});
db.getCollection('usedcars').find({'car_info.imgs':{$size:0}})
db.getCollection('usedcars').find({'car_info.imgs.0':{$exists:true}})
db.getCollection('usedcars').find({$where:'this.car_info.imgs.length>0'})