William Strong
William Strong

Reputation: 43

Filter array within MongoDB document

I am trying to create network map based of off twitter user mentions. I am storing the data in MongoDB and cannot figure out how to remove unwanted users.

Example db documents:

{
  'user': 'user1'
  'mentioned_users: ['user2', 'user3']
}
{
  'user': 'user2'
  'mentioned_users: ['user1', 'user3']
}

Example desired output:

{
  'user': 'user1'
  'mentioned_users': ['user2']
}
{
  'user': 'user2'
  'mentioned_users': ['user1']
}

user3 exists in both user1 and user2 list of mentioned users, however user3 is extraneous because user3 does not have their own document in the collection.

I need either a filter using db.collection.find() or other method so that I can get rid of all of the extraneous users.

Is there an easy way to do this with pymongo, or should I create a python solution?

Upvotes: 4

Views: 242

Answers (1)

Shaishab Roy
Shaishab Roy

Reputation: 16805

You can achieved that in MongoDB query usign aggregate. can try this one

db.users.aggregate([
  {$unwind: "$mentioned_users"},
  {$lookup: {from: "users", localField: "mentioned_users", foreignField: "user", as: "validUser"}},
  {$match: {"validUser.user": {$exists: true}}},
  {
    $group: {
      _id: "$_id",
      user: {$first: "$user"},
      mentioned_users: {$push: "$mentioned_users"}
    }
  }
])

Then output will be like

{
  "_id" : ObjectId("5a13bc87400096bfa0b34228"),
  "user" : "user1",
  "mentioned_users" : ["user2"]
}
{
  "_id" : ObjectId("5a13bc87400096bfa0b34229"),
  "user" : "user2",
  "mentioned_users" : ["user1"]
}

Upvotes: 1

Related Questions