开发者

find inside a hash mongodb

开发者 https://www.devze.com 2023-02-04 05:42 出处:网络
I have this struct in my collection: {foo : 1, bar : 4, baz : {a : 1, b : 2 ,c : \"fafofu\"}} How do I find \"a\" and \"b\" inside baz ?

I have this struct in my collection:

{foo : 1, bar : 4, baz : {a : 1, b : 2 ,c : "fafofu"}}

How do I find "a" and "b" inside baz ? It does not works db.my_collection.find({baz : {a : 1, b : 2});

I don't care about if开发者_StackOverflow中文版 "c" is "fafofu" or "cacocu" does not matters.


You can use . to reach into the baz object.

db.my_collection.find({"baz.a" : 1, "baz.b" : 2});


Perhaps if you try the following

{foo : 1, bar : 4, a: ["1"], b: ["2"],  "c": ["fafofu"]}

You could use find/findOne:

print(db.???.findOne( { c: "fafofu" } ).foo);

http://www.mongodb.org/display/DOCS/Full+Text+Search+in+Mongo

Unfortunately I don't have the ability to test this.

print(db.???.baz.findOne( { c: "fafofu" } ).foo);

My problem is how to find the data, becauase if you don't know the key it is difficult to optimize the performance of your search. Wouldn't you agree?

0

精彩评论

暂无评论...
验证码 换一张
取 消