zoukankan      html  css  js  c++  java
  • mongoDB文档操作

    document文档操作
    1、插入文档
    db.users.save({name: 'zhangsan', age: 25, sex: 1});
    db.users.insert({name: 'zhangsan', age: 25, sex: 1});
    插入单条数据 db.users.insertOne({});
    插入多条数据db.users.insertMany([{},{}])

    2、更新文档
    db.users.updaeMany({},{$set:{class:"1638"}})

    //false表示如果不存在不会插入,true表示如果有多个内容都更新(如果为false,则只更新第一条)
    db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true);
    相当于:update users set name = ‘changeName' where age = 25;
    db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);
    相当于:update users set age = age + 50 where name = ‘Lisi';
    db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true);
    相当于:update users set age = age + 50, name = ‘hoho' where name = ‘Lisi';
    3、获得当前db的所有文档
    查询所有记录
    db.userInfo.find();
    相当于:select* from userInfo;
    查询去重后数据
    db.userInfo.distinct("name");
    相当于:select distict name from userInfo;
    查询age = 22的记录
    db.userInfo.find({"age": 22});
    相当于: select * from userInfo where age = 22;
    查询age > 22的记录
    db.userInfo.find({age: {$gt: 22}});
    相当于:select * from userInfo where age >22;
    查询age < 22的记录
    db.userInfo.find({age: {$lt: 22}});
    相当于:select * from userInfo where age <22;

    查询age >= 25的记录
    db.userInfo.find({age: {$gte: 25}});
    相当于:select * from userInfo where age >= 25;
    查询age <= 25的记录
    db.userInfo.find({age: {$lte: 25}});
    查询age >= 23 并且 age <= 26
    db.userInfo.find({age: {$gte: 23, $lte: 26}});
    查询name中包含 mongo的数据
    db.userInfo.find({name: /mongo/});
    //相当于%%
    select * from userInfo where name like ‘%mongo%’;
    查询name中以mongo开头的
    db.userInfo.find({name: /^mongo/});
    select * from userInfo where name like ‘mongo%’;


    4、删除文档
    db.users.remove({age: 132},true);
    删除多条
    db.users.deleteMany({class:"1638"})
    删除所有
    db.users.remove({})
    5、简单查询文档
    查询指定列name、age数据
    db.userInfo.find({}, {name: 1, age: 1});
    相当于:select name, age from userInfo;
    查询指定列name、age数据, age > 25
    db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
    相当于:select name, age from userInfo where age >25;
    按照年龄排序
    升序:db.userInfo.find().sort({age: 1});
    降序:db.userInfo.find().sort({age: -1});
    查询name = zhangsan, age = 22的数据
    db.userInfo.find({name: 'zhangsan', age: 22});
    相当于:select * from userInfo where name = ‘zhangsan' and age = ’22';
    查询前5条数据
    db.userInfo.find().limit(5);
    相当于:select top 5 * from userInfo;
    查询10条以后的数据
    db.userInfo.find().skip(10);
    相当于:select * from userInfo where id not in (
    select top 10 * from userInfo
    );
    查询在5-10之间的数据
    db.userInfo.find().limit(5).skip(5);
    or与 查询
    db.userInfo.find({$or: [{age: 22}, {age: 25}]});
    相当于:select * from userInfo where age = 22 or age = 25;
    查询第一条数据
    db.userInfo.findOne();
    相当于:selecttop 1 * from userInfo;
    db.userInfo.find().limit(1);
    查询某个结果集的记录条数
    db.userInfo.find({age: {$gte: 25}}).count();
    相当于:select count(*) from userInfo where age >= 20;
    按照某列进行排序计数
    db.userInfo.find({sex: {$exists: true}}).count();
    相当于:select count(sex) from userInfo;

  • 相关阅读:
    ext DateTime.js在ie下显示不全
    js 获得每周周日到周一日期
    近十年one-to-one最短路算法研究整理【转】
    虚函数(实现多态)
    函数调用机制2
    函数调用机制
    面向对象的三大特性
    矩阵类c++实现
    矩阵求逆c++实现
    解决文件大小上传限制
  • 原文地址:https://www.cnblogs.com/shenjp/p/6407048.html
Copyright © 2011-2022 走看看