Node.js 与MongoDB&;构建最受欢迎的产品列表;猫鼬?

Node.js 与MongoDB&;构建最受欢迎的产品列表;猫鼬?,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我开始使用mongo,我想为用户“喜欢”的项目创建一个模式。我当前使用mongoose和node.js编写的代码如下所示: // load the things we need var mongoose = require('mongoose'); // define the schema for our favourites model var favouritedItemsSchema = mongoose.Schema({ userId : Number,

我开始使用mongo,我想为用户“喜欢”的项目创建一个模式。我当前使用mongoose和node.js编写的代码如下所示:

// load the things we need
var mongoose = require('mongoose');

// define the schema for our favourites model
var favouritedItemsSchema = mongoose.Schema({
    userId           : Number,
    item             : [{
        itemId       : Number,
        addedDate    : Date
    }]
});

// create the model for favourites and expose it to our app
module.exports = mongoose.model('Favourites', favouritedItemsSchema);

来自关系数据库的背景,我想知道上述方法是否代表了一种合适的NoSQL数据库设计方法?如果没有,有人能告诉我什么东西符合设计理念吗?

是的,你是对的,关系设计方法和NoSQL设计方法完全不同

例如,在RDBMS中有10个表,在mongo中可能只有2或3个集合。这是因为我们在对象之间创建关系的方式在NoSQL(子文档、数组等)中更有趣

这里有一个解决问题的方法,重用现有的用户集合

// load the things we need
var mongoose = require('mongoose');

// define the schema for our model
var userSchema = mongoose.Schema({
    username: string,
    favourites: [{
        id: Schema.Types.ObjectId,
        addedDate: Date
    }]
});

// export model
module.exports = mongoose.model('User', userSchema);

您还应该提到,这样做是一种不好的做法,因为您创建了收藏夹的无约束数组,而无约束数组在mongo中是反模式的/“无约束数组在mongo中是反模式的”-为什么@ArkadiiBerezkin@Abby因为不受约束的数组可能会使文档以不受约束的方式增长(换言之,无限增长),从而使索引变得更加困难。