Ideas how to query two collections in mongodb

Volodymyr Bilyachat

I have collection friends

- userId: user id
- Friends: array of user ids

And users

- _id: userId
- Last Login
- Other User info fields

Last login field is updated every 5 minutes if user is browsing my website.

Right now I have logic where I get user ids from friends from friend collection, then query user collection with that ids to get user info.

ADDED: In addition in future releases i would add that user will be able to add friends not only users but pets from other collection so friend embedded array will look like {UserId, PetId}

But for next release I would like to add new functionality where I would show friends sorted by last login.

Solutions which I think would work

  1. Map reduce on select -Easy to implement but would probably have problems with performance
  2. Map reduce on update - in this case i would probably use property "out" to write new collection on each update, so then i will have new collection Friend.reduced with all info i need, and then i can easy query it by indexes
  3. Add new property Last Update to Friends list collection so it will look like {FriendId, LastUpdate} logic would be easy to implement on Business level.

What other options to solve my issue?

zero323

And simple query like below won't work?

var someId = ObjectId("52758653cbd6ca816ca0ee1b")
var friends = db.friends.findOne({"userId": someId}, {"_id": 0, "friends": 1}).friends

db.users.find(
    {_id: {$in: friends }}
).sort({lastLogin: -1})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Two mongodb collections in one query

From Java

How to group two collections in one query with MongoDB or Mongoose

From Dev

How to query two collections that have a "one-to-many" relationship in MongoDB?

From Dev

ExpressJS Routing with Two MongoDB Collections Query

From Dev

How to query the whole collections including reference collections in MongoDB?

From Dev

compare two collections in mongodb using java or an simple query

From Dev

Merge two collections in MongoDB

From Dev

Aggregation of two collections in MongoDB

From Dev

How to Join two MongoDB Collections with the Unity JDBC driver?

From Dev

how to count number of distinct values of a field from two collections in mongodb

From Dev

How to find match elements in between two collections in mongodb?

From Dev

How can I join two collections with $in operator in mongodb?

From Dev

MongoDB Query across Multiple Collections

From Dev

Join two collections with MapReduce in MongoDB

From Dev

Two queries on different collections - MongoDB

From Dev

MongoDB and Spring: How to correctly query with two fields in one repository query?

From Dev

Comparing documents between two MongoDB collections

From Dev

Mongodb merge results from two collections

From Dev

How many MongoDB Collections required

From Dev

How do I use mongodb to count only collections that match two fields

From Dev

How do I use mongodb to count only collections that match two fields

From Dev

How to Union two collections efficiently?

From Dev

How to join two collections in mongoose

From Dev

how to create a mongodb query that finds if a number is between two fields?

From Dev

How to save multiple mongodb collections using Promise?

From Dev

How to remove not connected data in collections (mongoDB)

From Dev

How to Match on Joined Collections Using Laravel and MongoDB?

From Dev

What is the most efficient way to query multiple collections in MongoDB?

From Dev

Node, MongoDB, Mongoose Design Choice - Creating two collections or one collection

Related Related

  1. 1

    Two mongodb collections in one query

  2. 2

    How to group two collections in one query with MongoDB or Mongoose

  3. 3

    How to query two collections that have a "one-to-many" relationship in MongoDB?

  4. 4

    ExpressJS Routing with Two MongoDB Collections Query

  5. 5

    How to query the whole collections including reference collections in MongoDB?

  6. 6

    compare two collections in mongodb using java or an simple query

  7. 7

    Merge two collections in MongoDB

  8. 8

    Aggregation of two collections in MongoDB

  9. 9

    How to Join two MongoDB Collections with the Unity JDBC driver?

  10. 10

    how to count number of distinct values of a field from two collections in mongodb

  11. 11

    How to find match elements in between two collections in mongodb?

  12. 12

    How can I join two collections with $in operator in mongodb?

  13. 13

    MongoDB Query across Multiple Collections

  14. 14

    Join two collections with MapReduce in MongoDB

  15. 15

    Two queries on different collections - MongoDB

  16. 16

    MongoDB and Spring: How to correctly query with two fields in one repository query?

  17. 17

    Comparing documents between two MongoDB collections

  18. 18

    Mongodb merge results from two collections

  19. 19

    How many MongoDB Collections required

  20. 20

    How do I use mongodb to count only collections that match two fields

  21. 21

    How do I use mongodb to count only collections that match two fields

  22. 22

    How to Union two collections efficiently?

  23. 23

    How to join two collections in mongoose

  24. 24

    how to create a mongodb query that finds if a number is between two fields?

  25. 25

    How to save multiple mongodb collections using Promise?

  26. 26

    How to remove not connected data in collections (mongoDB)

  27. 27

    How to Match on Joined Collections Using Laravel and MongoDB?

  28. 28

    What is the most efficient way to query multiple collections in MongoDB?

  29. 29

    Node, MongoDB, Mongoose Design Choice - Creating two collections or one collection

HotTag

Archive