1
votes

I did a find on my mongodb and i can do this also in pymongo.

db.myTest.find({"cell":"k12p125"})

{ "_id" : ObjectId("5425194479df354955015855"), "cell" : "k12p125",
"fileInfo" : { "t50%" : 95, "fName" : "file1", "25%" : 67, "min" : 0, "50%" : 70, "std" : 23.155629597455047, "max" : 204, "75%" : 93, "count" : 42484, "mean" : 78.01871292721965 } }

{ "_id" : ObjectId("5425197d79df3549550161e9"), "cell" : "k12p125", "fileInfo" : { "t50%" : 92, "fName" : "file2", "25%" : 69, "min" : 0, "50%" : 71, "std" : 21.696940348452337, "max" : 204, "75%" : 90, "count" : 42484, "mean" : 78.14659636569061 } }

{ "_id" : ObjectId("542519b679df354955016b7d"), "cell" : "k12p125", "fileInfo" : { "t50%" : 95, "fName" : "file3", "25%" : 66, "min" : 0, "50%" : 70, "std" : 23.58083230514195, "max" : 205, "75%" : 94, "count" : 42484, "mean" : 77.98015723566519 } }

What I want to do is access embedded document. So I have a key called "cell" and the value i was looking for "k12p125". What I want to do is look for cell k12p125 and then pull out all the "std" key for each file and also the name of each file. So the information I would want is

cell:k12p125
fName:"file1",std:23.155629597455047
fName:"file2",std:21.696940348452337
fName:"file3",std:23.58083230514195

and be able to retrieve that information using pymongo. I would also like to know if there is a way to get the "std" of each file directly as long the file is sorted. I would want

cell:k12p125
std:23.155629597455047
std:21.696940348452337
std:23.58083230514195

The std that comes out will be in the sorted order of the files (std order as file1,file2,file3). Please let me know if you need more clarification. I am having hard time conceptualizing how to access embedded documents, so any help would be great. thanks!

1

1 Answers

0
votes

You can't get exactly your desired output with a basic query, but you can get close with a simple projection and a sort:

db.foo.find({"cell" : "k12p125"}, {cell : 1, "fileInfo.fName" : 1, "fileInfo.std" : 1, _id : 0}).sort({ "fileInfo.fName" : 1})
{ "cell" : "k12p125", "fileInfo" : { "fName" : "file1", "std" : 23.155629597455047 } }
{ "cell" : "k12p125", "fileInfo" : { "fName" : "file2", "std" : 21.696940348452337 } }
{ "cell" : "k12p125", "fileInfo" : { "fName" : "file3", "std" : 23.58083230514195 } }

Or, for reverse order:

db.foo.find({"cell" : "k12p125"}, {cell : 1, "fileInfo.fName" : 1, "fileInfo.std" : 1, _id : 0}).sort({ "fileInfo.fName" : -1})
{ "cell" : "k12p125", "fileInfo" : { "fName" : "file3", "std" : 23.58083230514195 } }
{ "cell" : "k12p125", "fileInfo" : { "fName" : "file2", "std" : 21.696940348452337 } }
{ "cell" : "k12p125", "fileInfo" : { "fName" : "file1", "std" : 23.155629597455047 } }

You still have the top level document of fileInfo to deal with, but usually that is pretty easy to deal with client side.

Then, for returning std, similarly the sorting can still be applied even if you are not projecting the sorted field:

db.foo.find({"cell" : "k12p125"}, {cell : 1, "fileInfo.std" : 1, _id : 0}).sort({ "fileInfo.fName" : -1})
{ "cell" : "k12p125", "fileInfo" : { "std" : 23.58083230514195 } }
{ "cell" : "k12p125", "fileInfo" : { "std" : 21.696940348452337 } }
{ "cell" : "k12p125", "fileInfo" : { "std" : 23.155629597455047 } }
db.foo.find({"cell" : "k12p125"}, {cell : 1, "fileInfo.std" : 1, _id : 0}).sort({ "fileInfo.fName" : 1})
{ "cell" : "k12p125", "fileInfo" : { "std" : 23.155629597455047 } }
{ "cell" : "k12p125", "fileInfo" : { "std" : 21.696940348452337 } }
{ "cell" : "k12p125", "fileInfo" : { "std" : 23.58083230514195 } }

You will want to establish what your query patterns are here so you can index appropriately. A compound index with cell as the leftmost element and a combination of the embedded fields will likely be what you need, but exactly what that will look like will depend on how you construct the queries in the end.