0

I have a document like this:

{
  project: 'Book',
  author: 'Author',
  pages: [{},{},{},{}]
}

for example I want to remove second element from pages array. I try to do something like this:

db.t.update(_id: "53296f43630a817c1af2a3e8"}, {$pull:{'test.$':1}})

but it isn't work for me.

1 Answer 1

1

Well the way you put it, it's going to be hard to match because there is nothing in there. But in the real world you would probably try to match like this:

db.t.update(
    { "_id": "53296f43630a817c1af2a3e8" }, 
    { "$pull": { "pages": { "value": 1 } }
)

That is assuming that there is a "value" property in the "sub-document".

But if you really do mean something like this where the is nothing to match, then try this:

db.t.update(
    { "_id": "53296f43630a817c1af2a3e8" }, 
    { "$set": { "pages.1": false } }
);

db.t.update(
    { "_id": "53296f43630a817c1af2a3e8" }, 
    { "$pull": { "pages": false } }
)

Which sets a value you can match and then matches and removes it.

Sign up to request clarification or add additional context in comments.

4 Comments

I need to remove, say, the fifth page of the book. I would not want to add a special page numbering field, as it seriously complicate future requests.
@АнтонШувалов Kristina's Book? It's old. Read the edit.
wat?) I don't understand you.
@АнтонШувалов Okay then. Glad you found that helpful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.