0

I'm got an empty array in my model that I am trying to push an array to but Compass does not show any changes to the document.

I've tried setting the type of my user_list property to array in my model, as well as just setting it as an empty array with []. I have also deleted and recreated the entire database as well to make sure it wasn't a memory issue.

Model:

const guildSchema = new Schema({
    guild_name: String,
    guild_id: { type: String, unique: true },
    ...
    user_list: []
})

Fetching the array and trying to push:

bot.on("guildUpdate", (oldGuild, newGuild) => {
    let userList = newGuild.fetchMembers().then(function(results){ console.log(results.members.keyArray()); userList = results.members.keyArray();
        Guild.findOneAndUpdate({guild_id: oldGuild.id}, {"$set": {"guild_name": newGuild.name, "guild_icon": newGuild.iconURL}}, {"$push": {"user_list": userList}}, function(err) {
            if (err) throw (err);
        });
    }).catch(console.error)
})

console.log(results.members.keyArray()) returns the simple array that I am trying to push just fine. Compass shows updates made with $set just fine as well. Something is going wrong when trying to push to user_list, as Compass continues to just show a blank array upon update.

Alexa Aria
  • 23
  • 4
  • Try this. bot.on("guildUpdate", (oldGuild, newGuild) => { let userList = newGuild.fetchMembers().then(function(results){ console.log(results.members.keyArray()); userList = results.members.keyArray(); Guild.findOneAndUpdate({guild_id: oldGuild.id}, {"$set": {"guild_name": newGuild.name, "guild_icon": newGuild.iconURL,"user_list": userList}}, function(err) { if (err) throw (err); }); }).catch(console.error) }) – Priyank Apr 02 '19 at 05:00
  • I believe the issue might be with the array declaration. Kindly post the sample value of an array. Mongoose supports arrays of SchemaTypes and arrays of subdocuments. So, you should use as needed. – kRiZ Apr 02 '19 at 05:36
  • ```[ '83041706965995520', '272595023651143681', '547137307535474719', '558039744718569492' ] ``` This is the log of the array I am trying to push. Just a set of strings. – Alexa Aria Apr 03 '19 at 22:30

1 Answers1

0

It is not working because you have added $push operator as a 3rd argument in the update query, It should be the part of 2nd argument of the update query, along with $set. #rd argument is meant for update query options like new, upsert

Try this:

Guild
    .findOneAndUpdate({
        guild_id: oldGuild.id
    }, {
        "$set": {"guild_name": newGuild.name, "guild_icon": newGuild.iconURL},
        "$push": {"user_list": userList} // should be a part of second argument
    }, function(err) {
        if (err) throw (err);
    });

And i believe you should define your array in the schema along with its type.

const guildSchema = new Schema({
    guild_name: String,
    guild_id: { type: String, unique: true },
    ...
    user_list: [String]//if type is String
})
Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52