MongoDb with auto increment and Find

59 views
Skip to first unread message

Itay Sena

unread,
Aug 19, 2014, 8:21:23 PM8/19/14
to mongod...@googlegroups.com
Hellol there, i have done this insert

db.dados.insert({
 
Dados:

 [{ _id: getNextSequenceDados("coddados"),

Dados:'comida16'}]
 

})

when i use the command db.dados.find()  i get
{ "_id" : 0, "seq" : 1 }
{ "_id" : 1, "Dados" : "comida16" }


now i'm trying to find the 'comida16' by the id value, that is 1

i'm using this command

db.dados.find({"Dados":{$elemMatch:{_id:1}}})

and this

db.data.find({"Data":{$elemMatch:{_id:{$gt:0}}}})

I get no return,

When i just had the field { "_id" : 1, "Dados" : "comida16" }, both codes worked before.

Tugdual Grall

unread,
Aug 20, 2014, 5:46:40 AM8/20/14
to mongod...@googlegroups.com
Hello Itay,

I am not sure to understand your use case.
  $elementMatch is related to array ( see  http://docs.mongodb.org/manual/reference/operator/query/elemMatch/ )
and you do not have any array

Also you are searching your document using the _id that is unique.. so the query
  db.dados.find( { _id : 1 }  );

This is based on the content of your dados collection. 

It looks like your insert is not doing what you expect it to do.... but I do not know what you expect.
So if what you expect as document is: 

{ "_id" : ObjectId("53f46dc14b1954038a4e8ccc"), "Dados" : [ { "_id" : 1, "Dados" : "comida16" } ] }

The following queries are working:

db.dados.find({"Dados":{$elemMatch:{_id:1}}})

db.dados.find({"Dados":{$elemMatch:{_id: {$gt : 0}  }}})

Tug
@tgrall

Itay Sena

unread,
Aug 20, 2014, 9:21:25 AM8/20/14
to mongod...@googlegroups.com

I'm confused, i did the insert via mongo shell and the codes i posted worked, but the insert on php are the code you posted....Thanks for your time and help Tug.
Reply all
Reply to author
Forward
0 new messages