Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

MongoDB: find and findOne with nested array filtering

This little task turned out to be harder than I thought.

Consider the following very simple Posts collection. Suppose I want to display all the posts, coupled with only the comments that were not deleted.

I.E. filter out the deleted comments from the comments array.

Since I have 100s of deleted comments per post, is there a way to do this server side?

The collection:

{
  "author": {},
  "message": "This is post1",
  "comments": [
    {
      "message": "Im number 1!!!",
      "state": {
        "deleted": false
      }
    },
    {
      "message": "YOU MOTHERF****R",
      "state": {
        "deleted": true
      }
    },
    {
      "message": "tHIS IS GREAT!",
      "state": {
        "deleted": false
      }
    },
    {
      "message": "I can type better than you guys",
      "state": {
        "deleted": false
      }
    }
  ]
},
{
  "author": {},
  "message": "This is post 2",
  "comments": [
    {
      "message": "This is bulls**t",
      "state": {
        "deleted": true
      }
    },
    {
      "message": "YOU MOTHERF****R",
      "state": {
        "deleted": true
      }
    },
    {
      "message": "I hate u!",
      "state": {
        "deleted": true
      }
    },
    {
      "message": "I wanna have your children",
      "state": {
        "deleted": false
      }
    }
  ]
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'll TL;DR since this turned out to be a hell of a ride. I've tried $elemMatch, I've tried $redact (also with $$CURRENT and $$ROOT), I've tried $map, I've tried the aggregation framework, I've tried $project.

You can read all about it here: https://www.devsbedevin.net/mongodb-find-findone-with-nested-array-filtering-finally/

TL;DR

The solution seems to be to use the aggregation framework to filter the array and override the comments property with the results. This is simpler than it sounds:

db.getCollection('posts').aggregate(
    {$match: {"author.id": authorId}},
    {$addFields : {"comments":{$filter:{ // We override the existing field!
        input: "$comments",
        as: "comment",
        cond: {$eq: ["$$comment.state.deleted", false]}
    }}}}
);

The result:

{
  "author": {},
  "message": "This is post1",
  "comments": [
    {
      "message": "Im number 1!!!",
      "state": {
        "deleted": false
      }
    },
    {
      "message": "tHIS IS GREAT!",
      "state": {
        "deleted": false
      }
    },
    {
      "message": "I can type better than you guys",
      "state": {
        "deleted": false
      }
    }
  ]
},
{
  "author": {},
  "message": "This is post 2",
  "comments": [
    {
      "message": "I wanna have your children",
      "state": {
        "deleted": false
      }
    }
  ]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...