mongodb crud

时间:2022-12-30 21:06:53
//添加数据
db.users.insert({"uName":"张三","age":,"gender":"男"});
db.users.insert({"name":"李四","wifes":[{"wname":"小崔"},{"wname":"小李"}]}); //查询数据
db.students.find();
//查询数据(带格式显示)
db.users.find().pretty(); //分页查询
db.books.insert({"bookName":"mybatis入门","price":19.9});
db.books.insert({"bookName":"spring","price":199.9});
db.books.insert({"bookName":"springmvc","price":69.9});
db.books.insert({"bookName":"mysql","price":39.9});
db.books.insert({"bookName":"python","price":59.9}); // skip(起始页码,从0开始) limit(每页显示数量)
db.books.find().skip().limit();
db.books.find().skip().limit(); //条件查询
//select * from books where price = 19.9
db.books.find({"price":19.9}); // price = ? and bookName = ?
db.books.find({"price":19.9,"bookName":"mybatis入门"}); //or
db.books.find({$or:[{"bookName":"mybatis入门"},{"bookName":"spring"},{"price":59.9}]}); // <
db.books.find({"price":{$lt:69.9}}) // <=
db.books.find({"price":{$lte:69.9}}); // >
db.books.find({"price":{$gt:69.9}}); // >=
db.books.find({"price":{$gte:69.9}}); // !=
db.books.find({"price":{$ne:69.9}}); // >=39.9 and <=69.9
db.books.find({"price":{$gte:39.9,$lte:69.9}}); //修改
//udpate book set price = ? where bookName = ?
db.books.update({"bookName":"mybatis入门"},{$set:{"price":119.99}});
//若根据修改条件 需要修改多条数据时,需要添加参数 multi:true
db.books.update({"bookName":"mybatis入门"},{$set:{"price":1.99}},{multi:true}); //删除
//delete book where bookName = ?
db.books.remove({"bookName":"spring"});