简体   繁体   English

Mongoose - 通过 Id 查找孩子

[英]Mongoose - find child by Id

Details: Here is my schema layout详细信息:这是我的架构布局

Schema Layout:
const mongoose = require("mongoose");

module.exports = function(db) {

let gameSchema = new mongoose.Schema({

opponent: String,
location: String,
date: String,
completions: Number,
attempts: Number,
yards: Number,
touchdowns: Number,
interceptions: Number,
});

let quarterbackSchema = new mongoose.Schema({
firstName: String, 
lastName: String,
school: String,
age: Number,
hometown: String,
games:[gameSchema]
});

Here is what a stored document looks like这是存储的文档的样子

{
"_id": {
    "$oid": "5c0a551df8555c1c1ef0c101"
},
"firstName": "Pepper",
"lastName": "Princess ",
"age": 14,
"hometown": "Farmville, Ms",
"school": "Farmers School of Important Stuff",
"games": [
    {
        "_id": {
            "$oid": "5c0ca5ba7213fe6a5f52848c"
        },
        "opponent": "Crock McSnagglebite",
        "location": "Pattys Putrid Flower Garden",
        "date": "1/23/2020",
        "completions": 777,
        "attempts": 777,
        "yards": 777,
        "touchdowns": 777,
        "interceptions": 777
    }
],
"__v": 1
}

Question: I'm trying to find the subdocument by its _id.问题:我试图通过 _id 查找子文档。 The example code I have posted displays the number as "5c0ca5ba7213fe6a5f52848c".我发布的示例代码将数字显示为“5c0ca5ba7213fe6a5f52848c”。

I have tried我试过了

 Quarterback.find({_id: req.body.id}).then(function(Results){});

 Quarterback.findOne({_id: req.body.id}).then(ect...

 Quarterback.findById({_id: req.body.id}).then(ect...

and many, many other ways, I simply don't know what I'm doing wrong.还有很多很多其他的方式,我根本不知道我做错了什么。 I used我用了

JSON.stringify(req.body)); JSON.stringify(req.body)); to verify the route was returning my desired value and it is验证路线是否返回了我想要的值,它是
my console.log outputs the following我的 console.log 输出以下内容

This is game details req.body:: {"child":"5c0d02649c936072d47f12a9"}这是游戏详情 req.body:: {"child":"5c0d02649c936072d47f12a9"}

but find keeps returning null for a result, when using the above methods.但是在使用上述方法时, find 一直为结果返回 null 。 I'm a student and still learning, so maybe I'm missing something easy.我是一名学生,仍在学习,所以也许我错过了一些简单的东西。 I'm hoping someone here can help.我希望这里有人可以提供帮助。 Thank you so much for your time!!非常感谢您的参与!!

There is an error in your quarterback schema.您的四分卫模式有误。 games:[gameSchema] should be games:[gameSchema]应该是

games:[game:{type: Schema.Types.ObjectId,ref: 'games'}] After correcting this, you should be able to run this query: Quarterback.find({games: req.body.id}).then(function(Results){}); games:[game:{type: Schema.Types.ObjectId,ref: 'games'}]更正后,您应该能够运行此查询: Quarterback.find({games: req.body.id}).then(function(Results){});

You may try你可以试试

const quarterback =  Quarterback.find({_id: req.body.quaterbackId})
const game = quarterback.games.id(req.body.gameId)
Quarterback.find({games._id: req.body.id}).then(function(Results){});

should work.应该管用。 You have to pass game._id because that's how JSON objects and properties are accessed您必须传递 game._id 因为这是访问 JSON 对象和属性的方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM