Handling updates to a Collection

Hello all,

I am trying to design a chatbot that stores its chat history in mongo. For example, if a user in slack asks the question ‘I need help with vpn’ the bot will respond to that message with helpful information, it will then ask if it was helpful and they have buttons to select yes or no. I want mongo to be able to store that information in a collection for what the bot heard, time timestamp, and if the user selected the yes/no button. I am using model.findOneandUpdate() but that only seems to update the first item in the collection, not the current message/collection.

Maybe there is a better way to do what I am trying to do. I am using node.js to handle the bot.

image

Hi @Matt_Wood ,

findOne operations weather with update or not only locate the first document in the filtered query.

If you need to update multiple records at a time you need a findAndUpdate command with multi : true .

Or updateMany commands.

Thanks
Pavel

How do I find and update the current collection?
Here is my current Schema
const MessageSchema = new Schema({

heard:{

    type: String,

    required: true

},

resolved:{

    type: String,

},

Timestamp: { 

    type: Date, 

    default: dateCentral

},

});

A user asks the question “help with vpn”
controller.hears(‘vpn’, ‘message,direct_message,mention,direct_mention’, async(bot, message) => {

    const Messages = new Msg ({heard:message.text});

    await Messages.save();

    await bot.replyEphemeral(message,{

        blocks: [ ... ]

Now the bot asks in the block if this information was helpful
I want the button answer (yes,no) to be added to the resolved part of the schema.
controller.on(‘block_actions’, async (bot, message) => {

if (message.text === 'No'){

    await Msg.findOneAndUpdate({resolved:message.incoming_message.text});

    console.log(message.incoming_message.channelData.actions[0].value)

    await bot.replyEphemeral(message, 'If you need further assistance please reach out to us on chat at  or call .');

} else if (message.text === 'Yes'){

    await Msg.findOneAndUpdate({resolved:message.incoming_message.text});

    console.log(message.incoming_message.channelData.actions[0].value)

    await bot.replyEphemeral(message, 'Great! Is there anything else I can assist you with today?');

}

});

Hi @Matt_Wood ,

Not super familiar with mongoose but ai guess the save method should respond with a document id created (_id in MongoDB).

I know the insertOne return it. Potentially you can manage your own id field and populate it with identifier .

Then in your find and update provide the specific id as part of the filter (_id : <updated id> )

This way you can update a specific single document. Again updating multiple documents with same value require a different method than the methods with “one” in name…

Thanks
Pav

Yes, I am looking to just update one collection/document so no need to update all of them. So you are saying to create a new object ID and then somehow update the response based on that objectID?

Hi,

Yes this is the main logic. You should grab the relevant unique identifier like _id and filter to hit that document…

When I try insertOne I get TypeError: Msg.insertOne is not a function

Looking into mongoose docs it seems to be called “create”

https://mongoosejs.com/docs/models.html#constructing-documents

Please refer to their docs or use the native node driver. As mentioned the mongoose is 3rd party therefore is not maintained or developed by MongoDB