0

edit:

Structure of document:

{"tags" : ["a"], "name" : "a", "creator" : "Nick here", "users" : [ "Nick here"], "connected" : 1, "ra" : 0.3967885102611035, "_id" : ObjectId("513c3e42071a103c09000003")}

I have problem with MongoDB $pull operator. It doesn't remove value from an array. Here's my query:

db.rooms.update({users: ["Nick here"]},{$pull: {users: ["Nick here"]}})

Here's how users field looks like:

(...),"users" : [ "Nick here" ],(...)


if I write it like this:

db.rooms.update({users: "Nick here"},{$pull: {users: ["Nick here"]}})

or

db.rooms.update({users: "Nick here"},{$pull: {users: "Nick here"}})

an error is returned:

Cannot apply $pull/$pullAll modifier to non-array


What is wrong in those queries? I want to pull/push users' nicks and remove document when 'users' field is empty.

Thanks in advance, Michał

2
  • Can you show the structure of the document? There is nothing wrong with this query db.rooms.update({users: "Nick here"},{$pull: {users: "Nick here"}}) Commented Mar 10, 2013 at 9:35
  • I've added document's structure edit: it seems that nick is being pulled properly now, I don't know what was wrong with this Commented Mar 10, 2013 at 10:52

1 Answer 1

1

I wrote test… Check you syntax. Query:

db.rooms.update({'users': 'Nick here'}, {$pull: {'users': 'Nick here'}})

May be in your query need use { multi: true }. For example:

db.rooms.update({'users': 'Nick here'}, {$pull: {'users': 'Nick here'}}, { multi: true })

Full log:

MongoDB shell version: 2.2.3
connecting to: so
> db.rooms.insert({"tags" : ["a"], "name" : "a", "creator" : "Nick here", "users" : [ "Nick here"], "connected" : 1, "ra" : 0.3967885102611035, "_id" : ObjectId("513c3e42071a103c09000003")} )
> db.rooms.findOne()
{
    "_id" : ObjectId("513c3e42071a103c09000003"),
    "tags" : [
        "a"
    ],
    "name" : "a",
    "creator" : "Nick here",
    "users" : [
        "Nick here"
    ],
    "connected" : 1,
    "ra" : 0.3967885102611035
}
> db.rooms.update({'users': 'Nick here'}, {$pull: {'users': 'Nick here'}})
> db.rooms.findOne()
{
    "_id" : ObjectId("513c3e42071a103c09000003"),
    "connected" : 1,
    "creator" : "Nick here",
    "name" : "a",
    "ra" : 0.3967885102611035,
    "tags" : [
        "a"
    ],
    "users" : [ ]
}
>
Sign up to request clarification or add additional context in comments.

1 Comment

That was not syntax problem. I just changed 'users' structure from single value field to array and forgot to remove documents from earlier tests with non-array here. Thanks anyway for your time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.