About update, push in Php

69 views
Skip to first unread message

Itay Sena

unread,
Apr 13, 2014, 4:34:45 PM4/13/14
to mongod...@googlegroups.com
Hello guys, i'm trying to insert in my collection a new field and value.


If one field that i'll insert doesn't exist, then it'll record it, if the field exist, then i'll update it and increment a new field and value, my question is, can i add another field with the same name?

If i can, i'm trying in php and all that i could was to update 2 fields and couldn't create one.

example:

food: "massa", taste: "good"

Lets say that the taste field is my opinion, and now i want to add the opinion of my friend, then it'd like

food: "massa", taste: "good", taste:"not good"


is that possible or i should use another name, like taste1 and so son?

David Hows

unread,
Apr 13, 2014, 8:59:07 PM4/13/14
to mongod...@googlegroups.com
Hi Itay,

You cannot have multiple keys with the same value in MongoDB, this would make it impossible to determine which of version of the two keys you wish to refer to.

If you wish to store multiple values under one key, you should use an array. You can then add values to the array with things like the $push operator: http://docs.mongodb.org/manual/reference/operator/update/push/

This would give you a document like:
food: "massa", taste: [ "good", "not good" ]


s.molinari

unread,
Apr 14, 2014, 3:23:08 AM4/14/14
to mongod...@googlegroups.com
Can you explain the problem you are trying to solve? You say opinions from yourself and your friend. Are these users of the system? Should their opinions be stored as opinions matching to them at any time or just counted as opinions about their taste in the food in general? It seems your logic is sort of stuck between these two variations.

Scott 

Itay Sena

unread,
Apr 14, 2014, 9:29:45 PM4/14/14
to mongod...@googlegroups.com
that key i used it not the _id, it's a key that i'll bring from a sql table , i'm using array and when i used push  (i didn't get it right) all that did was to update 2 fields, i'll explain it better if my answer to s.molinari

Itay Sena

unread,
Apr 14, 2014, 9:38:45 PM4/14/14
to mongod...@googlegroups.com
It's like this:
OBS:. i'm using mongo + php. but only in mongo will help me

I have a collection called words, and there will be a fields called  word: "house", id:"22", path:"\bin", startOfWord:"22", endOfWord:"27"

and i have a if that if the word i'll input is the same that i already have, it'll insert the fields, id, path, start, end with its respective values, but i can't seem to use push in that matter....

So my question is, it's possible to have field with same name and different  values?:

David Hows

unread,
Apr 15, 2014, 12:21:21 AM4/15/14
to mongod...@googlegroups.com
So my question is, it's possible to have field with same name and different  values?:

No. That is not possible.

Having read your suggestion, I have made the following:
db.qux.update({word:"house"},{ "$push": { path:"\\bin"}, $set: {"startOfWord":"22", "endOfWord":"27" }},true)
db
.qux.update({word:"house"},{ "$push": { path:"\\usr"}, $set: {"startOfWord":"22", "endOfWord":"27" }},true)
db
.qux.find();

{ "_id" : ObjectId("534cb354375536c8160a4376"), "word" : "house", "path" : [  "\\bin",  "\\usr" ], "startOfWord" : "22", "endOfWord" : "27" }

Would something like that be suitable? You can see that I use $push to add entries to the "path" variable.

Regards,
David
 

Itay Sena

unread,
Apr 15, 2014, 10:00:17 AM4/15/14
to mongod...@googlegroups.com
I see, if i do use push, i'll only add to the array, and not as a new field...man...i have no clue about what to do next =\

Itay Sena

unread,
Apr 18, 2014, 7:05:22 PM4/18/14
to mongod...@googlegroups.com
Well

Now I'm having another problem , i have this: db.comida.insert( { _id:1, "pratos":[ {comida:"Lasanha", preco:20.00}, {comida:"Panqueca", preco:15.00} ] } )

And i'm trying to update preco: 20.00 but i can't see to update it, i'm using this code

 db.comida.update( {_id: 4, "pratos.comida": "Lasanha"}, {$set: {"prato.$.preco": 12.99}} )


why can't i update it? =\

s.molinari

unread,
Apr 19, 2014, 1:16:19 AM4/19/14
to mongod...@googlegroups.com
Because "pratos" in the "set" is spelled wrong?

Scott

David Hows

unread,
Apr 20, 2014, 7:35:44 PM4/20/14
to mongod...@googlegroups.com
And because you are updating _id:4, when _id:1 is the document you inserted.
Message has been deleted
Message has been deleted
Message has been deleted

Itay Sena

unread,
Apr 21, 2014, 11:01:07 AM4/21/14
to mongod...@googlegroups.com
I'm sorry guys =\ I'm trying to code it to php and I'm all confused, Do you guys know php with mongo?


Em domingo, 13 de abril de 2014 17h34min45s UTC-3, Itay Sena escreveu:

Asya Kamsky

unread,
Apr 21, 2014, 7:22:11 PM4/21/14
to mongodb-user
I would recommend learning how to use MongoDB with PHP starting here:


Asya



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/0c433100-4f4e-40ef-8c6c-48cfaf06c915%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

s.molinari

unread,
Apr 22, 2014, 1:11:05 AM4/22/14
to mongod...@googlegroups.com
If you are also creating a larger application, I'd also suggest using a PHP framework that uses Doctrine ODM. Something like Symfony or Zend.

Scott


Itay Sena

unread,
Apr 23, 2014, 9:21:54 PM4/23/14
to mongod...@googlegroups.com
The problem is, i'm not very good with php, all i need is to translate the code for php...but i'm still trying...i'mm try until i can't see it =e

Itay Sena

unread,
Apr 23, 2014, 9:22:45 PM4/23/14
to mongod...@googlegroups.com
That's interesting, i'm trying to save a lot of data in one collection, so, i should use this framework?

s.molinari

unread,
Apr 24, 2014, 11:11:14 AM4/24/14
to mongod...@googlegroups.com
Hmm...you need to step back a few steps and just learn PHP and even programming first, if you don't know what a Framework is.

Scott

Itay Sena

unread,
Apr 28, 2014, 1:30:46 PM4/28/14
to mongod...@googlegroups.com
I see...sorry for this, i just know java, but i must do it in php, i'm trying to learn what i need, framework i know what it is, just don't know how to describe it.
But Thanks for your attention
Reply all
Reply to author
Forward
0 new messages