Projection option to return length/size of field

后端 未结 1 544
悲&欢浪女
悲&欢浪女 2021-01-22 00:13

I\'m simply trying to write a mongo query like in SQL:

SELECT LENGTH(binaryBody) AS bodyLength FROM documents;

To do so I thought I have to us

1条回答
  •  再見小時候
    2021-01-22 00:46

    .find() does not "alter" documents returned in any way. You can only "include" or "exclude" in projection.

    The only things that "alter" are .aggregate() or .mapReduce().

    For .aggregate(), requires MongoDB 3.4 for $strLenCP or $strLenBytes, but usually you mean the former:

    db.documents.aggregate([
      { "$project": {
        "bodyLength": { "$strLenCP": "$body" }
      }}
    ])
    

    For .mapReduce()

    db.documents.mapReduce(
      function() {
        emit(this._id, this.body.length)
      },
      function() { },
      { "out": { "inline": 1 } }
    );
    

    And realistically in the latter case, you may as well be iterating the cursor, and may need to unless the collection is small enough or you can actually output to another collection instead.

    The $size operator you are attempting to use only applies to "arrays" to return the number of entries present. And again, it's only valid for usage with the .aggregate() method.

    If you mean to omit characters such as a space within a string then a $split and $reduce with $concat can be applied:

    db.documents.aggregate([
      { "$addFields": {
        "bodyLength": {
          "$strLenCP": {
            "$reduce": {
              "input": { "$split": [ "$name", " "] },
              "initialValue": "",
              "in": { "$concat": [ "$$value", "$$this" ] }
            }
          }
        }
      }}
    ])
    

    Or again with mapReduce():

    db.documents.mapReduce(
      function() {
        emit(this._id, "".concat.apply(this.body.split(" ")).length)
        // Or even
        // emit(this._id, this.body.split(" ").reduce((o,e) => o.concat(e),"").length)
      },
      function() { },
      { "out": { "inline": 1 } }
    );
    

    0 讨论(0)
提交回复
热议问题