我试图访问嵌套在多个文档数组中的某个值。我已尝试各种语法来显示但无法找到方法。如何使用.find()获取值“aboveAve”?我需要进入注册 - >进入 - > GameStats
这是文档结构:
{
_id: ObjectId("5a665852f5b2abdbcdd190be"),
Name: "Beat Your Average",
MinPlayers: NumberLong("0"),
MaxPlayers: NumberLong("1"),
VisibleStartDateTime: ISODate("2018-01-22T20:27:40.497Z"),
StartDateTime: "2018-01-22T20:27:40.497Z",
VisibleEndDateTime: ISODate("2018-02-08T23:55:40.497Z"),
EndDateTime: "2018-02-08T23:55:40.497Z",
PayoutScale: NumberLong("20171004"),
PrizeFund: NumberLong("10000"),
EntryFee: NumberLong("500"),
Rake: NumberLong("500"),
Completed: true,
Live: false,
Registrations: [
{
UserId: NumberLong("231704"),
ContestId: "5a665852f5b2abdbcdd190be",
RegistrationDateTime: ISODate("2018-01-22T21:32:02.900Z"),
TimezoneOffset: NumberLong("300"),
PromisedBowlDate: "2018-01-22",
Entry: {
UserId: NumberLong("231704"),
TimezoneOffset: NumberLong("300"),
Games: [
NumberLong("125"),
NumberLong("125"),
NumberLong("135")
],
Pinfall: NumberLong("385"),
SeriesScore: NumberLong("385"),
Photos: [
"/home/rtb/uploads/a5cc578a3e4384ca4e7490e7b0af2fd19a99c6b6d09eab5fee2f1c45707fe38b/1516656862357_1c67eba6_3dd2_4e5a_9142_bc27602d64a6"
],
EntryDateTime: ISODate("2018-01-22T21:34:22.387Z"),
Average: NumberLong("95"),
EntryId: "29ba83cc-5ae1-49cf-b74f-c15b8bb17808",
Disqualified: false,
CenterId: NumberLong("2963"),
LeagueId: NumberLong("-1"),
Lane: NumberLong("6"),
Position: NumberLong("5"),
DisplayName: "JESS B",
RegistrationDateTime: ISODate("2018-01-22T21:32:02.900Z"),
Status: NumberLong("1"),
WinningsTotal: NumberLong("15"),
BonusAmount: NumberLong("35"),
TripleWin: "Tier 1",
Payout: NumberLong("5000"),
GameStats: [
{
game: "Game 1",
pinfall: NumberLong("125"),
aboveAve: NumberLong("30"),
prize: "$5"
},
答案 0 :(得分:0)
如果要返回包含aboveAve
字段的所有元素,则必须使用$exists
。由于您只想返回aboveAve
的值,因此您还需要指定要包含在投影中的字段。
db.collection.find({ "Registrations": { "Entry.GameStats": {"aboveAve": {$exists: true, $ne: null}}}}, {"aboveAve": 1, "_id":0 })