About stuck in program

I have a lot of schema but i want to take a record of each of changes in the document in one separate schema that keep record of each and every data…i don’t know how to do it

Hi @Kashif_Iqbal and welcome in the MongoDB Community :muscle: !

I don’t understand anything and it’s probably the same for everybody in here that want to help. It probably makes sense with all the context that you have but it’s very hard for us.

Please provide a few sample documents and the expected output. Eventually more context that would help understand what you are trying to do. This forum supports Markdown so you can share code blocks.

Cheers,
Maxime.

const auditSchema = new Schema({

baseUrl:{
    type: String,

},
ip:{
    type: String,
},
changeData:[{
    type: String,
}],
originalData:[{
    type: String,
}],
changedBy:{
    type: String,
    ref: 'users'
},
createdAt:{
    type: Date,
    default: Date.now,
},
updatedAt:{
    type: Date,
    default: Date.now,
}

})
sir suppose we have this audit document now i want to store each and every history of document and i don’t want to do this in my primary document so how it will done.
for example i have user and user updated some thing then after that i will save previous data and current data.
{
emailid: iqbal.kashif60@gmail.com,
name: kashf,
phoneNo.: 8864005925,
},
now after the update it will looks like
{
originalData: kashf
changeData: kashif
}

MongoDB Change Streams produce exactly the diff doc you are after. Then it’s up to you where you want to store it. Kafka could be a solution or another MongoDB collection.

You could use a Trigger in MongoDB Realm (which is using Change Streams in the background) and in the Realm Function, you could reinsert these change events in a new MongoDB collection. Only takes a few lines of code. Less than 5 I think.

Cheers,
Maxime.

thank u so much sir u saved my life and job

1 Like