简体   繁体   中英

Query data in one collection based on data in another collection in MongoDB

I am trying to learn how to use MongoDB and am really confused how to do this. What I have are two collections, one which has a number of users and another collection which has a number of items. For example:

users:

{
    "_id" : ObjectId("56dba03438e1a255b97e82b6"),
    "name" : "john",
    "age" : 25
}

items:

{
    "_id" : ObjectId("56dba0db38e1a255b97e82b7"),
    "name" : "pencil"
}

Now what I want to do in my app is to allow users to select an item but multiple users can select the same item. So I need to keep track of which users clicked which items. I thought about doing this using another collection which keeps track of the user id and item id (a user can only select an item once). Is this the correct approach? I created this collection:

useritems:

{ 
    "_id" : ObjectId("56dba0db38e1a255b97e82b7"),
    "userid" : "56db9fb038e1a255b97e82b5",
    "itemid" : "56dba03438e1a255b97e82b6"
}

If this is the right approach, then I want to be able to click on an item in my app and for it to display a list of all the users who selected that item. How can I do this? I got as far as to display only the useritems collection documents where the itemid = itemid selected on the app...but now how would I display all of the users from the users collection based on the ones in the useritems collection?

router.get('/userlist/:id', function(req, res) {  
    var db = req.db;  
    var collection = db.get('useritems');  
    collection.find({'itemid' : '_id'},{},function(e,docs){  
       res.json(docs);     
    });  
});  

Thanks for the help, I'm really having a hard time understanding how this would work.

The idea of creating a third collection is a solution that mirrors how you would solve this problem in a relational database. With MongoDB, it often pays off to think about different patterns based on how you access your data.

In your case, I would not create another collection, but track which user has selected which item within the user document, within the item document, or within both documents. Which way you do this depends on your data access patterns.

Adding Selected Item to User Document

{
    "_id": ObjectId("56dba03438e1a255b97e82b6"),
    "name": "john",
    "age": 25,
    "selectedItemId": "56dba0db38e1a255b97e82b7"
}

If you will often want to see the item each user has selected, it makes sense to store the item inside the user document. When you retrieve a user, you would only have to do one extra call to the items collection to retrieve the item for that user. (If you decide to use Mongoose as an object-document mapper (ODM), then you can achieve this extra call by using Mongoose's populate method ).

Adding User to the Item Document

{
    "_id": ObjectId("56dba03438e1a255b97e82b7"),
    "name": "pencil",
    "selectedBy": [
        "56dba0db38e1a255b97e82b4",
        "56dba0db38e1a255b97e82b5",
        "56dba0db38e1a255b97e82b6"
    ],
}

If you will often want to see which users have selected a given item, it makes sense to store an array of users inside the item document. When you retrieve an item, you would then have the IDs of the users that selected that item, which you could then retrieve from the database. (Again, if you decide to use Mongoose you can do this by using its populate method ).

Adding Both Solutions

The reason why you would prefer one solution over another is that given your access pattern, you will be spared from iterating through the whole collection to get the data you need. For example, in the case were you add the array of users to an item, if you wanted to find the item a given user has selected, you would have to iterate though all the items and look in the array of user IDs until you found the user you wanted. Something similar would occur if you only stored the item ID inside a user document and suddenly needed to look at all the users for a given item. If both of these calls are made often, then it pays off having the data in both places. Indeed this "denormalises" your data and you will have to make sure that when you insert, update, and delete the data you do so in both places, but it's a far more scalable solution if you're making both types of queries often.

Embedding the Whole Item Document inside the User Document

{
    "_id": ObjectId("56dba03438e1a255b97e82b6"),
    "name": "john",
    "age": 25,
    "selectedItem": {
        "name": "pencil"             
    }
}

Following the OP's comment, I'll address this scenario too. This is also a possible solution and can be very useful in simplifying the query needed to access the data. Just by querying the user document, you will be able to access what item he/she has selected without the extra query to a collection of items. The limitation of this approach is that if for whatever reason you want to update the name of the item from say "pencil" to "Pencil" , you will have to ensure that you update it across all of the user documents, otherwise your data will be inconsistent. This gets more complicated when your embedded documents are more complex. Nonetheless, it is a widely used solution. If you're rarely updating your data, but reading it very often, especially if you are more interested in seeing the item picked by a given user, then it definitely speeds up your most frequent data access patterns.

You are right, only you need populate the userid to get all atributes of that collection. I suggest you use (if your are not) Mongoose

With mongoose:

UserItems
  .find({'itemid' : '_id'})
  .populate('userid')
  .then( useritems => {
     // here you have all users with their data for a specific item
     return res.json(useritems);
  });

You can add an array in the item document that keeps track of the IDs of the users who clicked that item. This is assuming the ID is stored in a active session.

docs.user_who_clicked.push(req.user._id); docs.save()

I wouldn't create a separate collection just for that unless you have a good reason. Just add a selectedBy to each Item document. Also, I find it simpler to just use my own unique names or IDs rather than looking things up with the internal Mongo IDs. Something like this:

var items = db.collection('items');                                                                                                                
items.updateOne({itemname:'nuts'},{$push:{selectedBy:'johns'}});                                                                                   
//...                                                                                                                                              
items.find({itemname:'nuts'}).toArray(function(err,items) {                                                                                        
  console.log(items[0].selectedBy);                                                                                                                
  db.close();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM