Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6909037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:38:18+00:00 2026-05-27T08:38:18+00:00

I have a Mongo collection called documents which has the following structure: { name:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:38:19+00:00Added an answer on May 27, 2026 at 8:38 am

    let’s consider these datas :

    > db.nicolas.find()
    { "_id" : ObjectId("4edfd6551757be3330db7c75"), 
              "tokens" : [ { "id" : 1 }, { "id" : 2 }, { "id" : 3 } ] }
    { "_id" : ObjectId("4edfd50452d5d16ccc1aa7b5"), 
              "tokens" : [ { "id" : 3 }, { "id" : 4 }, { "id" : 5 } ] }
    

    If I apply these transformation :

    > db.nicolas.update({_id: ObjectId("4edfd6551757be3330db7c75")}, 
                        {$pull:{tokens: { id :{$nin:[1,2]}} } })
    

    I’ll get the following :

    > db.nicolas.find()
    { "_id" : ObjectId("4edfd6551757be3330db7c75"), 
              "tokens" : [ { "id" : 1 }, { "id" : 2 } ] }
    { "_id" : ObjectId("4edfd50452d5d16ccc1aa7b5"), 
              "tokens" : [ { "id" : 3 }, { "id" : 4 }, { "id" : 5 } ] }
    

    So, for your case, something like this should work :

    > db.documents.update({name: "foo"}, 
                          {$pull: {tokens: { _id : {$nin:['foo','bar']}} } }) 
    

    Does it work ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of the following sort of documents (taken from Mongo DB
I have a Mongo collection where each document has a set of unique embedded
I have a collection which is an action log between two users. It has
I'd like to query a mongo collection for records which either don't have a
If I have a Company collection which contains embedded Divisions: { _id: 1 _t:
I have a number of different documents in a mongo collection. The attrs are
I have a collection in mongo where objects appear as follows: { name:Scott, bio:Stumped,
I have a collection called contract and I would like to group using a_id
I have got mongo db using only one collection and consuming average cpu usage
I have an mongo query that looks like the following: {'schedule': {'$elemMatch': {'time': {'$gt':

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.