how to remove the element in an array according to index

852 views
Skip to first unread message

simon

unread,
Mar 12, 2012, 3:39:08 AM3/12/12
to mongodb-user
i have a table as the following:

{
username:"test1",
messages
[
{"date":123456,"content":"1 message"}
{"date":123456,"content":"2 message"}
{"date":123457,"content":"3 message"}
{"date":123457,"content":"4 message"}
]

}

now.I want to remove a element that's Index value is 2...how to do...

simon

unread,
Mar 12, 2012, 3:41:40 AM3/12/12
to mongodb-user
There are some errors..

Sam Millman

unread,
Mar 12, 2012, 5:11:13 AM3/12/12
to mongod...@googlegroups.com
You need to $unset the array element to null and the $pull the null value:

So to unset: update({$unset: { 'messages.2': 1 }}
Then to pull null: update({$pull: { 'messages': null }})



--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
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.


simon

unread,
Mar 12, 2012, 5:36:11 AM3/12/12
to mongodb-user
thank you..

On 3月12日, 下午5时11分, Sam Millman <sam.mill...@gmail.com> wrote:
> You need to $unset the array element to null and the $pull the null value:
>
> So to unset: update({$unset: { 'messages.2': 1 }}
> Then to pull null: update({$pull: { 'messages': null }})
>
Reply all
Reply to author
Forward
0 new messages