【问题标题】:MONGODB How many models are necessary to make a wish list?MONGODB 制作愿望清单需要多少个模型?
【发布时间】:2020-04-26 13:33:03
【问题描述】:

好的,让我列一个愿望清单。您注册/登录,您可以将产品添加到列表中。我来自 MYSQL,所以如果我要在 MYSQL 中执行此操作,我会这样做。

表:用户名

id|username|password

表格:愿望清单

id|userid|wish

然后我会使用用户 ID 调用愿望清单。但是 mongoDB 不同,因为它使用了json。对于mongoose 模型,我会这样做吗?

{
username
password
wishlist{
  productname
  bla bla
}
}

我该如何设置?我在 react-redux 中这样做。我已经通过身份验证做了一些事情,您可以在其中注册和添加/删除愿望,但问题是所有用户的所有产品都在同一页面上。目前我有两个独立的应用模型。

【问题讨论】:

    标签: json reactjs mongodb mongoose redux


    【解决方案1】:

    这是一个示例(请记住,您不必定义 id 字段,因为 mongo 会自动添加一个):

    -- mongodb.js--

    const mongoose = require('mongoose');
    
    // Connect to database. (replace username, password and dbname).
    mongoose.connect('mongodb://username:password@localhost:27017/dbname', {
        useNewUrlParser: true,
        useFindAndModify: false,
        useCreateIndex: true,
        autoIndex: false,
        useUnifiedTopology: true
      })
      .then(() => console.log('Connected to mongodb...'))
      .catch(err => console.error(err));
    
    // Define user schema.
    const UserSchema = new mongoose.Schema({
      username: {
        type: String,
        unique: true,
        index: true
      },
      password: String
    });
    
    // Define virtual field ( it "connects" two collections ( collections are like tables in MYSQL ) ).
    UserSchema.virtual('wishlists', {
      ref: 'Wishlists',
      localField: '_id',
      foreignField: 'userid'
    }, {
      toObject: {
        virtuals: true
      },
      toJSON: {
        virtuals: true
      }
    });
    
    // Define wishlist schema with field "userid" that references a user in "Users" collection.
    const WishlistSchema = new mongoose.Schema({
      userid: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Users'
      },
      wish: String
    });
    
    // Create collections called "Users" and "Wishlists" and export for use.
    module.exports = {
      User: mongoose.model('Users', UserSchema),
      Wishlist: mongoose.model('Wishlists', WishlistSchema)
    };

    然后您可以在您的 NodeJS 服务器(您的 API)中使用它,使用这些行作为示例:

    const Models = require('./mongodb.js');
    const User = Models.User;
    const Wishlist = Models.Wishlist;
    
    // Add new.
    await new User({
      name: name,
      password: password
    }).save();
    
    // Find user by id.
    await Users.findById('someid')
      .select();
    
    // Find wishlists by userid and find user ( but keep out password! ).
    await Wishlist.find({
      userid: 'someid'
    }).select().populate('userid', 'username');
    
    // Find user and all wishlists associated (using virtual fields from mongodb ).
    await Users.findById('someid')
      .select().populate('wishlists');

    【讨论】:

    • 非常感谢。我是 mongoDB 的新手。这很有帮助。
    猜你喜欢
    • 2022-01-21
    • 2019-09-25
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 2020-02-11
    相关资源
    最近更新 更多