zoukankan      html  css  js  c++  java
  • MongoDB 数组操作符与运算操作符

    数组操作

    创建包含数组和嵌套数组的文档

    > db.accounts.insert({ name:"alice2", balance:100,contact:[13611111111,"Guangzhou"] })
    WriteResult({ "nInserted" : 1 })
    > db.accounts.insert({ name:"alice2", balance:100,contact:[[13611111111,13622222222],"Guangzhou"] })
    WriteResult({ "nInserted" : 1 })
    

    查询数组元素与嵌套数组

    > db.accounts.find({contact:{$all:[13611111111]}})
    { "_id" : ObjectId("5d7f8f0da7b5867462dee50f"), "name" : "alice2", "balance" : 100, "contact" : [ 13611111111, "Guangzhou" ] }
    > db.accounts.find({contact:{$all:[[13611111111]]}})
    > db.accounts.find({contact:{$all:[[13611111111,13622222222]]}})
    { "_id" : ObjectId("5d7f8f21a7b5867462dee510"), "name" : "alice2", "balance" : 100, "contact" : [ [ 13611111111, 13622222222 ], "Guangzhou" ] }
    

    筛选数组元素符合特定条件的记录

    > db.accounts.find({contact:{$elemMatch:{$gt:10000000000}}})
    { "_id" : ObjectId("5d7f8f0da7b5867462dee50f"), "name" : "alice2", "balance" : 100, "contact" : [ 13611111111, "Guangzhou" ] }
    

    regex 运算操作符

    # i 选项,不区分大小写
    > db.accounts.find({name:{$regex:/^A/,$options:'i'}})
    { "_id" : "account1", "name" : "alice2", "balance" : 100 }
    { "_id" : ObjectId("5d7f8f21a7b5867462dee510"), "name" : "alice2", "balance" : 100, "contact" : [ [ 13611111111, 13622222222 ], "Guangzhou" ] }
    

    配合 $in 使用的时候,无需使用 $regex 关键字

    > db.accounts.find({name:{$in:[/^A/i,/^D/i]}})
    { "_id" : "account1", "name" : "alice2", "balance" : 100 }
    { "_id" : ObjectId("5d7f25f4aecbd2bc0fa821b4"), "name" : "david", "balance" : 200 }
    
  • 相关阅读:
    leetcode 268. Missing Number
    DBSCAN
    python二维数组初始化
    leetcode 661. Image Smoother
    leetcode 599. Minimum Index Sum of Two Lists
    Python中的sort() key含义
    leetcode 447. Number of Boomerangs
    leetcode 697. Degree of an Array
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(1月3日)
    北京Uber优步司机奖励政策(1月2日)
  • 原文地址:https://www.cnblogs.com/zy108830/p/12639606.html
Copyright © 2011-2022 走看看