Discussion:
Cannot upsert with $addToSet field in the query
chris.chen21
2011-09-25 02:32:28 UTC
Permalink
Schema looks something like this:

{_id: ... , aliases: [...]}

The index:

db.songs.ensureIndex({"aliases":1},{'unique':true});

When running a query like this:

$col->update(
{ '_id' => '123' , 'aliases'=>'fat tony'},
{
'$addToSet' => { 'aliases' => 'fat tony' },
},

{ 'upsert' => 1, }
);

I get

Cannot apply $addToSet modifier to non-array

Is there a way to tell mongodb in the upsert query that 'aliases'
should be an array containing 'fat tony' if it doesn't exist?
Karl Seguin
2011-09-25 02:55:55 UTC
Permalink
Not sure I understand why...seems strange, but it looks like it works if you
replace the query part with:

{ '_id' => '123' , 'aliases'=> {'$in' => ['fat tony']}},

There must be some sort of link between the query and the update with
respect to the type it assumes an unknown field is..or something like that.
Seems buggy to me, but that work around does work.

Curious, does $addToSet really add any value when you have an unique index
on the array? you could just use $push (though, it suffers the same
strangeness, so I guess it really doesn't change much).
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/BdMGPRSmoxwJ.
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
Eliot Horowitz
2011-09-26 06:37:45 UTC
Permalink
This is definitely an issue:
Made a case: https://jira.mongodb.org/browse/SERVER-3946
Post by Karl Seguin
Not sure I understand why...seems strange, but it looks like it works if you
{ '_id' => '123' , 'aliases'=> {'$in' => ['fat tony']}},
There must be some sort of link between the query and the update with
respect to the type it assumes an unknown field is..or something like that.
Seems buggy to me, but that work around does work.
Curious, does $addToSet really add any value when you have an unique index
on the array?  you could just use $push (though, it suffers the same
strangeness, so I guess it really doesn't change much).
--
You received this message because you are subscribed to the Google Groups
"mongodb-user" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/mongodb-user/-/BdMGPRSmoxwJ.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/mongodb-user?hl=en.
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to mongodb-user+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
chris.chen21
2011-09-28 09:58:18 UTC
Permalink
Thanks for the workaround! Yea I'm not sure if $addToSet is still
necessary in this case.
Post by Karl Seguin
Not sure I understand why...seems strange, but it looks like it works if you
{ '_id' => '123' , 'aliases'=> {'$in' => ['fat tony']}},
There must be some sort of link between the query and the update with
respect to the type it assumes an unknown field is..or something like that.
Seems buggy to me, but that work around does work.
Curious, does $addToSet really add any value when you have an unique index
on the array?  you could just use $push (though, it suffers the same
strangeness, so I guess it really doesn't change much).
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to mongodb-user+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
Loading...