4

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?

1 Answer 1

1

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"]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.