简体   繁体   中英

Query data in one collection based on ObjectId in another collection in MongoDB Ionic React

I try to show one collection field based on the ObjectId that's related to another collection in MongoDB. I have 3 collections:

Users:

{
    "_id" : "115ds1f4sd55fe1e51fds5f4",
    "name" : "Sam",
    "age" : 25
}

Country:

{
    "_id" : "654564dsf65g4d1e51fds5f4",
    "userId" : ObjectId ("115ds1f4sd55fe1e51fds5f4"),
    "country" : "New Zealand"
}

{
    "_id" : "8247dddsf65g4d1e51fds5f4",
    "userId" : ObjectId ("115ds1f4sd55fe1e51fds5f4"),
    "country" : "Australia"
}

Address:

{
    "_id" : "6184s68f4se65f4se6d545ee",
    "CountryId" : ObjectId("654564dsf65g4d1e51fds5f4"),
    "userId" : ObjectId ("115ds1f4sd55fe1e51fds5f4"),
    "address" : "12345 Main Street",
    "city" : "Auckaland"
}

{
    "_id" : "6184s68f4se65f4se6d545ee",
    "CountryId" : ObjectId("654564dsf65g4d1e51fds5f4"),
    "userId" : ObjectId ("115ds1f4sd55fe1e51fds5f4"),
    "address" : "12345 Main Street",
    "city" : "Wellington"
}

{
    "_id" : "6184s68f4se65f4se6d545ee",
    "CountryId" : ObjectId("8247dddsf65g4d1e51fds5f4"),
    "userId" : ObjectId ("115ds1f4sd55fe1e51fds5f4"),
    "address" : "12345 Main Street",
    "city" : "Sydney"
}

Now on the app that I practice on, I use Swiper Slider Module to display the Country this user lived, now I want to show all the Addresses this user has under the carousel depending on which country is displayed in the Swiper slider.

For example: If the slider is in Australia, the IonCard only show 1 address in Sydney. If the slider is in New Zealand, the IonCard shows 2 addresses of Auckland & Wellington.

If anyone has any idea that will be awesome!

I have a feeling you are looking for more than this, can you provide additional information if that is true

db.collection.find( { CountryId: { $eq: ObjectId("654564dsf65g4d1e51fds5f4") } } )

https://docs.mongodb.com/manual/reference/method/db.collection.find/

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