【问题标题】:How to manage two MongoDB Collections in react-native如何在 react-native 中管理两个 MongoDB 集合
【发布时间】:2021-08-09 04:03:03
【问题描述】:

我想制作两个系列。一张给Users,一张给他们的Transactions

我的 Schema.js:

const mongoose = require('mongoose')
const adsSchema = new mongoose.Schema({
    item : String,
    server : String,
    price : String,
})

mongoose.model("ads",adsSchema)

我的 app.js:

const Ads = mongoose.model("ads")

app.post('/send-data',(req,res) => {
    const ads = new Ads({
        item : req.body.item,
        price : req.body.price,
    })
    ads.save()
    .then(data => {
        console.log(data)
        res.send("success")
    }).catch(err => {
        console.log(,err)
    })
})

所以我需要制作另一个 app.post 吗?喜欢app.post('/transactions',...

【问题讨论】:

  • 您可以在User 架构中为其事务添加一个对象。示例:Transactions: {...}
  • @Tyler2P ,我不想混淆它们。事务有它自己的规则和对象

标签: mongodb react-native express mongoose mongoose-schema


【解决方案1】:

只需在用户架构中引用事务架构。 示例:

 const userSchema = Schema({
      ...
      transactions: [{ type: Schema.Types.ObjectId, ref: 'Transaction' }]
    });

您也可以反过来,在事务模式中引用用户。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-10
    • 2020-01-31
    • 2021-10-02
    • 2017-08-10
    • 2020-09-03
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多