I have a mongodb object that looks something like this:
{
"_id" : "5a567ef6992fb9020474f72d",
"groupName" : "test, admin, binky, water, brick",
"users" : [
"test",
"admin",
"binky",
"water",
"brick"
],
"comments" : [
{
"comment" : "test message",
"createdBy" : "admin",
"createdAt" : ISODate("2018-01-11T00:01:59.672Z")
},
{
"comment" : "test message",
"createdBy" : "admin",
"createdAt" : ISODate("2018-01-11T00:02:48.237Z")
}
],
"createdBy" : "admin"
}
So, my problem is that I don't want the number of comments to grow infinitely. I want to be able to limit the number of possible comments.
Is there anyway to look at the number of comments when inserting, then maybe delete the first few over 100? So, let's say we get to 101, it would delete the first object???
My code for adding a comment is:
var coll = DatabaseCommon.Instance.GetDatabase("ps_" + siteId).GetCollection<Group>("chatgroups");
var currentGroup = coll.FindOneAndUpdate(y => y._id == groupId, Builders<Group>.Update.Push<Comment>(y => y.comments, comment));
I've tried:
var coll = DatabaseCommon.Instance.GetDatabase("ps_" + siteId).GetCollection<Group>("chatgroups");
coll.UpdateOne({ _id: groupId }, { $push: { comments: { $each: [comment], $slice: -100 } } });
but I always get an error when compiling, saying that it's expecting another close parens.
