欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mongodb数据库基础练习记录

程序员文章站 2022-06-09 23:09:45
mongodb基础练习记录 db.getcollection('foo').find().pretty(); db.getcollection('foo').find...

mongodb基础练习记录

db.getcollection('foo').find().pretty();

db.getcollection('foo').findone();

db.getcollection('foo').find({"hello":"world"});

db.getcollection('foo').find({"hello":{$ne:"world"}});

//$lt;

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时删除一条