mongodb的CRUD操作详解2-find

时间:2022-07-25
本文章向大家介绍mongodb的CRUD操作详解2-find,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

查询操作

比较运算符

英文

数学符号

$lt

Lower Than

<

$lte

Lower Than or Euqal

<=

$gt

Greater Than

>

$gte

Greater Than or Equal

>=

$ne

Not Equal

!=

逻辑运算符

含义

$or

或运算

$in

元素 In 集合(数组)

$nin

元素 not In 集合(数组)

$not

取反

//查询集合内所有文档,pretty格式化查询结果
db.inventory.find( {} )
db.inventory.find()
db.inventory.find().pretty()
//等于条件查询
db.inventory.find( { item: "ABC1" } )
//查询符号查询
//https://docs.mongodb.org/manual/reference/operator/query
db.inventory.find( { item: { $in: [ 'ABC1', 'ABC2' ] } } )
//AND条件查询
db.inventory.find( {  'details.model': '14Q3', 'stock.qty': { $lt: 26} } )
//OR条件查询
db.inventory.find(
    {
    $or: [ {  'stock.qty': { $gt: 25} }, { price: { $lt: 6} } ]
    }
)
//AND和OR组合查询
db.inventory.find(
    {
    category :'clothing',
   $or: [ {  'stock.qty': { $gt: 25} }, { price: { $lt: 6} } ]
    }
)
//嵌套文档查询producer属性值匹配
db.inventory.find(
{
        stock:
        {
        size: 'M',
        qty: 5
        }
}
)
db.inventory.find( { 'stock.size': 'M' } )

嵌套属性的查询注意加上单引号

数组匹配查询

初始化数据

db.inventory.insert([{ _id: 5, type: "food", item: "aaa", ratings: [ 5, 8, 9 ] },
{ _id: 6, type: "food", item: "bbb", ratings: [ 5, 9 ] },
{ _id: 7, type: "food", item: "ccc", ratings: [ 9, 5, 8 ] }])

db.inventory.insert([{
    _id: 100,
    type: "food",
    item: "xyz",
    qty: 25,
    price: 2.5,
    ratings: [ 5, 8, 9 ],
    memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
},
{
    _id: 101,
    type: "fruit",
    item: "jkl",
    qty: 10,
    price: 4.25,
    ratings: [ 5, 9 ],
    memos: [ { memo: "on time", by: "payment" }, { memo: "delayed", by: "shipping" } ]
}])

数组查询

//数组精确匹配查询,数组元素个数,等值
db.inventory.find( { ratings: [ 5, 8, 9 ] } )
//数组ratings包含元素5的查询
db.inventory.find( { ratings: 5 } )
//数组ratings的地一个元素是5
db.inventory.find( { 'ratings.0': 5 } )
//数组ratings有一个元素大于5并且小于9,就匹配整条文档
db.inventory.find( { ratings: { $elemMatch: { $gt: 5, $lt: 9 } } } )
//数组ratings的每个元素都必须满足大于5*或*小于9,就返回整条文档
db.inventory.find( { ratings: { $gt: 5, $lt: 9 } } )


//数组memos的第一个嵌套对象的属性by匹配是shipping
db.inventory.find( { 'memos.0.by': 'shipping' } )
返回如下结果:
{
    _id: 100,
    type: "food",
    item: "xyz",
    qty: 25,
    price: 2.5,
    ratings: [ 5, 8, 9 ],
    memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}


//数组memos任意一个嵌套对象属性by匹配是shipping
db.inventory.find( { 'memos.by': 'shipping' } )
返回如下结果:
{
    _id: 100,
    type: "food",
    item: "xyz",
    qty: 25,
    price: 2.5,
    ratings: [ 5, 8, 9 ],
    memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}
{
    _id: 101,
    type: "fruit",
    item: "jkl",
    qty: 10,
    price: 4.25,
    ratings: [ 5, 9 ],
    memos: [ { memo: "on time", by: "payment" }, { memo: "delayed", by: "shipping" } ]
}


//至少有一个memos嵌套对象同时匹配memo和by属性
db.inventory.find(
    {
        memos:
        {
            $elemMatch:
            {
            memo: 'on time',
            by: 'shipping'
            }
        }
    }
)
返回结果记录如下:
{
    _id: 100,
    type: "food",
    item: "xyz",
    qty: 25,
    price: 2.5,
    ratings: [ 5, 8, 9 ],
    memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}

喜欢 (1)or分享 (0)