mongo04---基本查询

核心:
mongod: 数据库核心进程
mongos: 查询路由器,集群时用
mongo:   交互终端(客户端)

二进制导出导入:
mongodump:导出bson数据
mongorestore: 导入bson
bsondump: bson转换为json
monoplog:

数据导出导入
mongoexport: 导出json,csv,tsv格式
mongoimport: 导入json,csv,tsv

诊断工具:
mongostats
mongotop
mongosniff  用来检查mongo运行状态


1: 创建数据库目录, 如 /data/db , 
2: ./mongodb --dbpath /data/db 
3: mongodb要求磁盘剩余空间>=4G,
如果不够,可以用 --smallfiles 选项


1:插入单条记录,不指定主键
db.collectionName.insert({name:'lisi',age:28});
2: 插入单条记录,指定主键
db.collctionName.insert({_id:3,name:'lisi',age:28});
3: 插入多条记录
db.collctionName.insert([
{_id:4,name:'wangwu',age:60}
]);


Update时可用的操作符
例:
->db.user.insert({name:'lisi',age:12,sex:'male',height:123,area:'haidian'});
->db.user.update({name:'lisi'},{$set:{area:'chaoyang'},$unset:{height:1},$inc:{age:1},$rename:{sex:'gender'}});
> db.user.find();
{ "_id" : ObjectId("51fc01c4f5de93e1f2856e33"), "age" : 13, "area" : "chaoyang", "gender" : "male", "name" : "lisi" }

$setOnInsert ->相当于mysql中的列的默认值




$gt:>,$gte:>=,$in=in,$lt:<,$lte:<=,$ne:!=,$nin:not in,$all:所有,$or:or,$andand,$not:not,$nor:都不成立,$exists:某列存在,$mod:取模,$type:数据为某类型,$where,$regex:正则表达式,$inc:增长,$rename:重命名列,$setOnInsert,$set:设置字段的新值,$unset:删除指定的列



查询表达式:
db.goods.find().count()//31
db.goods.find({goods_id:32})//goods_id=32的

1: 最简单的查询表达式
{filed:value} ,是指查询field列的值为value的文档
2: $ne --- != 查询表达式
{field:{$ne:value}}
作用--查filed列的值 不等于 value 的文档
3: $nin --> not in
4: $all
语法: {field:{$all:[v1,v2..]}} 
是指取出 field列是一个数组,且至少包含 v1,v2值
5: $exists 
语法: {field:{$exists:1}}
作用: 查询出含有field字段的文档
6: $nor,
{$nor,[条件1,条件2]}是指  所有条件都不满足的文档为真返回

------------------------------------------------------

db.goods.find(
    {
        cat_id:{$ne:3},                //cat_id不等于3
        shop_price:{$gt:1000},      //shop_price大于1000
        shop_price:{$lte:100}},        //低于或等于
        market_price:{$in:[4,11]},  //4或者11
        goods_name:"KD876",               //goods_name="KD876"
        $and:
            [
                {shop_price:{$gt:100}},{shop_price:{$lt:1000}}
            ],                           //价格介于100--1000
    },
    {
        cat_id:1,   //查cat_id,goods_name列,不查_id
        goods_name:1,
        _id:0
    }
)
------------------------------------------------------
1.7:取出不属于第3栏目且不属于第11栏目的商品($and $nin和$nor分别实现)
 db.goods.find(
 {
     $and:[   //and是并且
         {cat_id:{$ne:3}},    //不等于3
         {cat_id:{$ne:11}}    //不等于11
     ]
 },
 {
     goods_name:1,
     cat_id:1
 }
 )
 ------------------------------------------------------
 db.goods.find(
    {
        goods_id:{$mod:[5,0]}        //对哪个列做判断,取模,对5取模为0
    },  
    {                              //取那几列
        goods_id:1,
        goods_name:1,
        _id:0
    } 
 )
 
 ------------------------------------------------------
 db.goods.find(
    {
        age:{$exists:1}            //有age列的json文档,colletion的每一行就是一个bson,json列结构不一样,
    }
)
 ------------------------------------------------------
 db.stu.insert(name:"lily",hobby:['a','b','c'])
 db.stu.insert(name:"lucy",hobby:['e','b','c'])
 db.stu.find(
 {
    hobby:{$all:['b','c']}   //所指定的内容都要有
 }
 )   //2条都出来
 db.stu.find(
 {
    hobby:{$all:['q','c']} 
 }
 )   //没有
 ---------------------where要把二进制转换为json对象---------------------------------
 db.goods.find(
    {
        $where:'this.cat_id ==3 && this.cat_id ==4'    //用where把二进制文件转换为一个json对象,不用where是直接二进制比较,where的效率低
    }
 )
 
  db.goods.find(
    {
        $where:'this.shop_price > 300 && this.shop_price < 400 || this.shop_price > 3000 && this.shop_price < 4000'
    }
 )
-----------------正则表达式效率也低-------------------------------------
db.goods.find(
    {
        goods_name:{$regex:/^诺基亚.*/}   //诺基亚开头的
    }
)
 ------------------------------------------------------
 db.goods.find(
 {
    hobby:{$type:2}   //查询age列是字符串类型的,double:1,string:2,object:3,Array:4,boolean:8,date:9,null:10,
 }
 )
 ------------------------------------------------------
 db.goods.find(
 {
    cat_id:{$nin:[3,11]}
 },
 {
     goods_name:1,
     cat_id:1
 }
 );
 ------------------------------------------------------
 db.goods.find(
 {
     $nor:[    //都不成立
         {cat_id:3},
         {cat_id:11}
     ]
 },
 {
     goods_name:1,
     cat_id:1
 }
 );


1.8:取出价格大于100且小于300,或者大于4000且小于5000的商品()
db.goods.find(
    {
        $or:    //2个or打中括号,对多列限制
        [
            {    //每一个打大括号
                $and:
                [        //2个and打中括号
                    {shop_price:{$gt:100}},        //每一个打大括号,对一列限制
                    {shop_price:{$lt:300}}
                ]
            },
            {
                $and:
                [
                    {shop_price:{$gt:4000}},
                    {shop_price:{$lt:5000}}
                ]
            }
        ]
    },
    {
        goods_name:1,
        shop_price:1
    }
);



1.9:取出goods_id%5 == 1, 即,1,6,11,..这样的商品
db.goods.find({goods_id:{$mod:[5,1]}});


1.10:取出有age属性的文档
db.stu.find({age:{$exists:1}});
含有age属性的文档将会被查出

------------------------------------------------------
use shop
for(var i=0;i<10000;i++){
    db.bar.insert({_id:i++,title:'hello'+i});
}   //迅速在mongo里面插入10000条数据
db.bar.find().count();
------------------------------------------------------
游标cursor分页查询:
通俗的说,游标不是查询结果,而是查询的返回资源,或者接口.
通过这个接口,你可以逐条读取.就像php中的fopen打开文件,得到一个资源一样, 通过资源,可以一行一行的读文件.
</pre>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值