mongodb 点和美元符号的含义

Cod*_*ree 5 mongodb pymongo

我正在调试使用flask_pymongo的代码

我看到这段代码

   dabb = mongo.db.tbl.find({
            "transactions": {'$elemMatch': {"from":{'$elemMatch':{"from":str(to)}},"to":{'$elemMatch':{"to":str(frm)}}}}
        },{"transactions.$": 1 })
Run Code Online (Sandbox Code Playgroud)

我不明白 {"transactions.$": 1 } 是做什么的。

在此输入图像描述

MIA*_*ARD 7

位置 $ 运算符标识数组中要更新的元素,而无需显式指定该元素在数组中的位置。

它限制数组的内容返回:

  1. 数组中第一个符合查询条件的元素。
  2. 如果没有为数组指定查询条件,则为第一个元素(从 MongoDB 4.4 开始)。
    dabb = mongo.db.tbl.find({
            "transactions": {
                 '$elemMatch': {
                          "from":{
                             '$elemMatch':{ "from":str(to) }
                                 },
                          "to": {
                             '$elemMatch':{"to":str(frm)}
                                 }
                              }
                           }          // Only apply for first element then meet query condition within an array
                         },          //  when a element meet the query condition 
          {"transactions.$": 1 })   //   it will only return that element from array
                                  
Run Code Online (Sandbox Code Playgroud)

在获取转换数组中满足查询条件的第一个元素后,它将返回该元素。请参阅包含大量示例的文档

对于您的情况,此链接更合适: