无法在 MongoDB (mongoose) 文档中追加数组

Unable to Append Array Within MongoDB (mongoose) Document

我正在尝试在 mongo 数据库中附加一个空数组,其中包含在前端(网站 ui)创建的字符串,相关代码片段如下:

mongo架构

    email: String,
    displayName: String,
    googleId: String,
    toIgnore: [{toIgnoreURL: String}]
})

用passport & passport-google-oauth20

创建文件
User.findOne({email: email.emails[0].value}).then((currentUser)=>{
            if(currentUser){
                // user does already exist
                console.log('welcome back!', currentUser)
                done(null, currentUser)
            }
            else{ // user doesn't exist yet
            new User({
                email: email.emails[0].value,
                displayName: email.displayName,
                googleId: email.id,
                toIgnore: []
            }).save().then((newUser)=>{
                console.log('new user created: ' + newUser)
                done(null, newUser)
            });
            }
        })

最后尝试附加 'User' 集合(当前登录用户的)的 toIgnore 数组 属性

User.update(
        {email: emailThisSession},
        {$push: {toIgnore: {toIgnoreURL: url}}})

在mongodb中看到如下文件创建成功

_id
:ObjectId(
IdOfDocumentInMongoDB)
toIgnore
:
Array
email
:
"myactualtestemail"
googleId
:
"longgoogleidonlynumbers"
__v
:
0

(也如附图所示) document in mongodb ui

我似乎不知道如何实际填充 'toIgnore' 数组。 例如,当控制台记录以下

var ignoreList = User.findOne({email:emailThisSession}).toIgnore;
console.log(ignoreList)

输出为undefined 请注意,控制台记录 url 变量确实打印了我想附加到数组的值! 我在 Schema builder 和文档创建中尝试了我能想到的任何格式组合,但我找不到正确的方法来完成它! 任何帮助将不胜感激!

更新,使用 promises 也不行

User.findOne({email:emailThisSession}).then((currentUser)=>{ //adding .exec() after findOne({query}) does not help as in User.findOne({email:emailThisSession}).exec().then(...)
            console.log(currentUser.toIgnore, url) //output is empty array and proper value for url variable, empty array meaning []
            currentUser.toIgnore.push(url)
        });

同时调整架构如下:

const userSchema = new Schema({
    email: String,
    displayName: String,
    googleId: String,
    toIgnore: []
})

解决方案

我只需要将更新命令更改为

User.updateOne(
            {email: emailThisSession},
            {$push: {toIgnore: {toIgnoreURL: url}}}).then((user)=>{
                console.log(user)
            })

谢谢@yaya!

Unable to add array element within a document with mongoose

  1. 将您的架构定义为:
const UserSchema = new mongoose.Schema({
  ...
  toIgnore: [{toIgnoreURL: String}]
})
  1. 然后你可以像这样创建一个对象:
new User({
  ...,
  toIgnore: [] // <-- optional, you can remove it
})
  1. 检查值:
User.findOne({...}).then(user => {
  console.log(user.toIgnore)
});
  1. 您的更新语句应该是:
User.update(
  {email: emailThisSession},
  {$push: {toIgnore: {toIgnoreURL: url}}}
).then(user => {
  console.log(user)
})

所以在你的情况下,这是未定义的:

User.findOne({email:emailThisSession}).toIgnore

因为 findOne 是异步的。为了获得结果,您可以将其传递给回调,也可以使用 promises (User.findOne({...}).then(user => console.log(user.toIgnore)))

更新:

While adjusting the Schema as follows: new Schema({..., toIgnore: []})

这是您更新的问题。你应该把它改回:toIgnore: [{toIgnoreURL: String}]