【发布时间】:2016-01-27 08:36:18
【问题描述】:
我在我的 Web 应用程序中使用 Mongoose 和 Passport 将新用户添加到我的 MongoDB 数据库中。我使用 Google oauth 进行注册/登录。在我的用户架构中,我为 google 方法定义了以下内容:
//user.js
var userSchema = mongoose.Schema({
google : {
id : String,
token : String,
access_token : String,
email : String,
name : String,
picture : String,
nameInfo : Object,
}
});
我使用以下方法创建用户:
//passport.js
var newUser = new User();
//newUser.google.token = token;
newUser.google.name = profile.displayName;
newUser.google.nameInfo = profile._json.name
newUser.google.email = profile.emails[0].value;
newUser.google.id = profile.id;
newUser.google.picture = profile._json.image.url + '0';
newUser.google.access_token = token;
您可以看到所有这些数据都在我的用户文档顶层的“google”数组下。我将如何添加一个新的、复杂的静态数组?例如,我想要一个新数组位于文档的顶层,格式如下:
newUser.dogs = ["cats":[]}]
我需要这种特定格式,这取决于用户在我的 Web 代码中的外观。我最终会将数据添加到“cats”数组中,但它需要从空开始。当我尝试这种变化时,我只能获得顶级数组 - 例如:我的文档看起来像:
objectid: 1000,
google: [...],
dogs: []
当我需要它的样子时:
objectid: 1000,
google: [...],
dogs: [{"cats":[]}]
我需要对我的架构进行哪些更改,无论是架构设计还是进入架构的数据?
【问题讨论】:
标签: node.js mongodb express mongoose passport.js