【问题标题】:Getting the property from the active slide in MongoDB Ionic React从 MongoDB Ionic React 中的活动幻灯片中获取属性
【发布时间】:2022-01-05 22:46:43
【问题描述】:

我尝试根据与 MongoDB 中的另一个集合相关的 ObjectId 显示一个集合字段。我有 3 个收藏:

用户:

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

国家:

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

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

地址:

{
    "_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"
}

现在在我练习的应用程序上,我使用 Swiper Slider Module 来显示此用户居住的国家/地区,现在我想根据 Swiper 滑块中显示的国家/地区显示此用户在轮播下拥有的所有地址。

例如: 如果滑块位于澳大利亚,则 IonCard 仅显示悉尼的 1 个地址。如果滑块在新西兰,IonCard 会显示奥克兰和惠灵顿的 2 个地址。

如果有人有任何想法,那就太棒了!

这是我用来在滑动滑块中获取国家/地区的函数:

useEffect(() => {
        async function fetchCountry() {
            const client = app.currentUser?.mongoClient("mongodb-atlas");
            const collection = client?.db("database").collection("country");
            return collection?.find({ userId: new ObjectId(app.currentUser?.id) });
        }

        if (loadingCountry) {
            fetchCountry().then((r) => {
                setCountry(r);
                setLoadingCountry(false);
            });
        }
    }, [loadingCountry]);

我也想使用类似的函数来获取地址,但不确定如何从 country 函数中检索 countryId。 这是我使用的:

return collection?.find({ countryId: new ObjectId("654564dsf65g4d1e51fds5f4") });

它会显示来自这个 countryId 的地址,但是当幻灯片切换到另一个国家时它不会改变。

【问题讨论】:

    标签: reactjs typescript ionic-framework mongodb-realm


    【解决方案1】:

    我感觉您正在寻找的不止于此,如果这是真的,您能否提供更多信息

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

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

    【讨论】:

    • 嗨亚伦,谢谢你的回答!我已经添加了我之前尝试过的更多细节,主要是实现当滑块在新西兰时,IonCard 只显示新西兰的地址,当滑块在澳大利亚时,它将显示澳大利亚的地址。
    • 这仍然不足以让我理解,因为我在代码中只看到一个查询
    猜你喜欢
    • 2021-11-27
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 2019-09-20
    相关资源
    最近更新 更多