zoukankan      html  css  js  c++  java
  • 7.2MongoDB之语法风格

    7.2MongoDB之语法风格

    MongoDB接入JavaScript风格语法

    使用for循环往集合中插入数据

    for (var i = 0; i < 5; i++) {
    db.getCollection("practicegrammar").insert(
    {
    "name":"Jun" + i,
    "age":18 + i,
    "wife":"no"
    }
    )
    }

    1、while:循环。

    2、hasNext: cursor集合遍历,是否还有数据。

    3、printjson:输出集合中的文档

    4、next:当前文档,并向下遍历。

    forEach循环输入,必须定义一个函数供每个游标元素调用。

    db.getCollection("practiceaggregate").find().forEach(printjson)

    将文档中的对象以json串的形式分组打印出

    hasNext: cursor集合遍历,是否还有数据

    var cursor = db.getCollection("practiceaggregate").find()
    printjson(cursor[1])

    创建了一个cursor变量,这个变量相当于把集合里面的域当成了数组,cursor是这些域的索引下标

    可以直接打印出对应索引下标的json对象信息:

    printjson(cursor[2])
    # 如果索引大于数组内对象数量会返回变量未初始化

    游标也可以当作数组来用

    db.getCollection("practiceaggregate").find().toArray()

    返回一个集合拥有的对象所有信息以数组的方式展现

    MongoDB中高级查询

    面向文档的NoSql数据库重要解决的问题不是高性能的并发读写问题,而是保证海量数据存储的同时,具有比一般数据库更加良好的查询性能

    $all

    匹配所有,类似关系型数据库中的in,但是关系型数据库中的in是满足括号里面的任何一个都能出数据,而mongodb中的$all则必须满足[]中的所有值。

    构造数据:

    var ins = {
    "name":"Jun",
    "age":[20,21,22],
    "wife":"none"
    }
    db.getCollection("practicegrammar").save(ins)

    执行$all语句进行匹配查询:

    db.getCollection("practicegrammar").find(
    {
    age:{
    $all:[21,22]
    }
    }
    )

    $exists

    判断字段是否存在,(true/false)

    构造数据:

    var testdata={
    "name":"Jun",
    "age":18,
    "city":"China"
    }
    db.getCollection("practicegrammar").insert(testdata)

    执行$exists语句进行判断:

    db.getCollection("practicegrammar").find(
    {
    city:{
    $exists:true
    }
    }
    )
    # {city:{$exists:true}}: 集合中存在city这个字段的数据

    $mod

    取模运算

    执行$mod语句:

    db.getCollection("practicegrammar").find(
    {
    age:{
    $mod:[7,6]
    }
    }
    )

    {age:{$mod:[7,6]}}:集合中模7余6的数据

    这里面如果你选择的域存在数组结构的数据并不会将条件带到数组中的数据去验证。

    $ne(not equal)

    不等于

    实例:

    db.getCollection("practicegrammar").find(
    {
    "name":{
    $ne:"Jun"
    }
    }
    )

    $in包含,$nin不包含。

    跟MySQL中的in,not in一样

    {age:{$in:[20,22]}}:如果age是数组的话,只要数组包含in中条件的任何一条数据,都能被检索出来。不是数组,则只要满足in中的任何一个条件数据,也可以被检索出来。

    db.getCollection("practicegrammar").find(
    {
    age:{
    $in:[20,22]
    }
    }
    )

    $size

    数组元素个数

    {age:{$size:4}}:age数组元素个数为4的数据结果集

    db.getCollection("practicegrammar").find(
    {
    age:{
    $size:4
    }
    }
    )

    $not正则匹配

    不包含以...开头的数据

    查询field中包含 mongo的数据 相当于%%--->MySQL中域内容包含mongo的

    db.getCollection().find({name: /mongo/})

    查询name中以mongo开头的--->相当于--->select * from table_name where coloumn like ‘mongo%’;

    db.getCollection().find({name: /^mongo/})
    # 注意内部//里面有一个"^"符号

    查询指定域field1、field2的数据 相当于--->select coloumn1,coloumn2 from table_name

    db.getCollection().find({}, {field1: 1, field2: 1})
    # 可以用true或false
    # 如果用false就是排除field1,显示field1以外的域信息

    查询某个结果集的记录条数--->相当于--->select count(*) from table_name where field >= 20;

    db.getCollection.find({field1: {$gte: 25}}).count()

     

    It's a lonely road!!!
  • 相关阅读:
    Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/g++4.2 failed with exit code 1
    Mark !
    delphi 与 C++ 进程之间的命名管道通信
    delphi 获取网页所有链接并访问赚取金币
    char类型转化为string类型 string(int n, char c)
    游戏接入支付宝遇到的一些问题
    mac下versions 提交提示 SVN Working Copy xxx locked
    上周项目遇到的问题
    使用elementPlus组件,分页功能[Pagination]时,字段:total="XXX"时,实际样式显示为 Total {total} 问题的解决办法
    Vue3+Module功能+指定Getter模块+获取不到资源
  • 原文地址:https://www.cnblogs.com/JunkingBoy/p/14964643.html
Copyright © 2011-2022 走看看