I have a Mongo collection called documents which has the following structure:
{
name: "..."
tokens: [
{
_id: <string (unique)>,
tf: <integer>,
tf_idf: <float>
},
...
]
}
I have a list of tokens that I want to keep. Is it somehow possible to build up a query using $pull to delete all tokens whose id is not contained in the list?
The following does not work:
db.documents.update({name: "foo"}, {
$pull : {
$not : {
$elemMatch : { 'tokens' : { '_id' : ['foo', 'bar'] }}
}
}
})
The only alternative I see so far is iterating over all documents and checking on the client whether or not to keep the token and eventually delete them one by one. I’d rather use an approach which does not involve pulling all data to the client just to check which tokens can be deleted and which not.
let’s consider these datas :
If I apply these transformation :
I’ll get the following :
So, for your case, something like this should work :
Does it work ?