mongodb练习记录

db.getCollection('foo').find().pretty();
db.getCollection('foo').findOne();
db.getCollection('foo').find({"hello":"world"});
db.getCollection('foo').find({"hello":{$ne:"world"}});
//$lt;<<br>db.getCollection('user_test').find({"age":{$lt:"28"}});//查询集合为user_test,条件为年龄小于28的记录
//$lte; <=
db.getCollection('user_test').find({"age":{$lt:"28"}});//查询集合为user_test,条件为年龄小于等于28的记录
//$gt; >
db.getCollection('user_test').find({"age":{$gt:"28"}});//查询集合为user_test,条件为年龄大于28的记录
//$gte; >=
db.getCollection('user_test').find({"age":{$gte:"28"}});//查询集合为user_test,条件为年龄大于等于28的记录
//$ne; !=
db.getCollection('user_test').find({"age":{$ne:"28"}});//查询集合为user_test,条件为年龄不等于28的记录

db.getCollection("foo").find({"hello":"world","hello":"world"});
select * from foo where hello="world" AND hello = "world";

db.getCollection("foo").find({$or:[{"hello":"world"},{"hello":"world2"}]});

db.getCollection("foo").find({"hello":"world",$or:[{"hello":"world"}]});


//mysql like
db.getCollection("foo").find({"hello":{$regex:"world2"}});
db.getCollection("fooo").find({"hello":/world2/});
db.getCollection("foo").find({"hello":/^(world)|(world2)$/i});

db.getCollection("user_test").find().sort({"name":-1}).skip(0).limit(10);//查询集合为user_test,排序key为name排序方式为-1倒序,skip(0).limit(10)从0到10
//插入记录
db.user_test.insert({name: '张三',
 
   age:'18',
    sex:'男',
    favorite:['mongodb', 'database', 'NoSQL'],
})//插入集合为user_test中
db.user_test.insert({name: '张三',
    age:'18',
    sex:'男',
    favorite:{"sports":{"ball":"basketball","games":"fire"},"books":{"animal":"动物世界","cars":"汽车世界"}},
})
db.user_test.insert({name: '张三',
    age:'18',
    sex:'男',
    favorite:{"sports":{"ball":"basketball","games":"fire"},"books":{"animal":"动物世界","cars":"汽车世界"}},
})
db.user_test.insert({name: '张三',
    age:'18',
    sex:'男',
    favorite:{"sports":{"ball":"basketball","games":"fire"},"books":{"animal":"动物世界","cars":"汽车世界"}},
})

db.user_test.insert({name: '张三',
    age:'18',
    sex:'男',
    favorite:{"sports":"running,swiming"},
})

db.user_test.update({"name":"张三"},{$set:{"favorite.sports.ball":"footerball"}});//修改一条name为张三的favorite.sports.ball值为footerball
db.user_test.update({"name":"张三"},{$set:{"favorite.sports.ball":"footerball"}},{multi:true});//修改所有name为张三的favorite.sports.ball值为footerball
db.user_test.remove({"name":"张三"});//删除所有name为张三的记录
db.user_test.remove({"name":"张三"},1);//删除一条name为张三的记录,remove的第二个参数为1时删除一条

猜你喜欢

转载自blog.csdn.net/u011504963/article/details/79321667