M
M
Messi2019-09-20 09:35:22
Doctrine ORM
Messi, 2019-09-20 09:35:22

How to make a connection in Mongo?

Hello! I use MongoDB with Symfony and need a hint, there are two collections:
Collection1: _id, total, card_id, images
Collection2: _id, data, price,
card_id link on collection2 via card_id?
Who knows, tell me, please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2019-09-20
@FitTech

I didn’t do exactly this to access from one collection to another not by _id, but apparently so
https://stackoverflow.com/questions/19287142/popul...

Coll1.virtual('card_id', {
  ref: 'Coll2',
  localField: 'card_id',
  foreignField: 'card_id',
  justOne: true // for many-to-1 relationships
});

Coll1.find({...})
    // if you use select() be sure to include the foreign key field !
    .select({ card_id }) 
    // use the 'virtual population' name
    .populate('card_id')
    .exec(function(err, books) {...})

this is for laying mongoDB for node.js, but if it’s possible there, then it’s possible directly through mongo, I just don’t know how
if both documents refer to the Card collection by _id (and most likely they did, these card IDs are also on refer to something?), then it’s quite easy to register a reference to the _id of the card in the collections and, when requested, receive json with the card
https://docs.mongodb.com/manual/reference/database...
https://mongoosejs.com/ docs/populate.html
but I can't help with PHP, I have no idea how it works
-
also look at this, probably what you need
https://docs.mongodb.com/manual/reference/operator...
Collection1.aggregate([{
   $lookup:
     {
       from: "Collection2",
       localField: "card_id",
       foreignField: "card_id",
       as: "from_coll_2"
     }
}])

I'm not sure about the syntax from my head, but you can try

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question