0

Greetings,

I have the following mongodb object:

{
   "_id": ObjectId("4d0b9c7a8b012fe287547157"),
   "messages": {
     "0": {
       "toUname": "Eamorr3",
       "fromUname": "Eamorr2",
       "time": 1292606586,
       "id": "j7zwr2hzx14d3sucmvp5",
       "subject": "asdf",
       "message": "asdf",
       "read": 0
    }
  },
   "uname": "Eamorr3"
}

How do I set "read" to 1 in PHP?

Here's what I've tried sofar to no avail:

update(array('uname'=>$uname),array('$set'=>array('messages'=>array('read')=>'1')));

I'm totally stuck. Any help much appreciated.

Many thanks in advance,

1 Answer 1

2

I believe it's either

update(array('uname'=>$uname),array('$set'=>array('messages.0'=>array('read')=>'1')));

or

update(array('uname'=>$uname),array('$set'=>array('messages.0.read'=>1));
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for your answer! Do you know how I can search through the array by specifying the id (j7zwr2hzx14d3sucmvp5 in this case) rather than "messages.0". I meant to ask that in the original question...
Take a look at the "The Positional Operator" section at the bottom of php.net/manual/en/mongo.updates.php - looks like what you're looking for.

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.