如何在 Mongoose 中使用 find where 查询

时间:2021-01-04 03:25:31

标签: mongodb express mongoose

我最近写了很多 MySQL,所以我忘记了如何编写一些 Mongoose 查询。我正在尝试搜索我的数据库以查找用户与 req.params.user 相同的股票。下面是路线和我的架构,所以你可以看到发生了什么。我在 Post man 中运行它,但它给了我一个“无法获取 /stock/user-stocks/Sina”错误。我还在我的数据库中留下了样本股票以供参考

router.get('user-stocks/:user', (req, res) => {
    Stock.find({ user: toString(req.params.user)})
    .then((stocks) => res.json(stocks))
    .catch(err => res.json(err))
})

const stockSchema = new Schema({
    user: {type: String, required: true},
    symbol: {type: String, required: true},
    target: {type: Number, required: false},
    stop: {type: Number, required: false},
    description: {type: String, required: false},
    date: {type: Date, required: false}
})

{
        "_id": "5ff284d07b93cd434cae9a08",
        "user": "Sina",
        "symbol": "AAPL",
        "target": 200,
        "stop": 100,
        "description": "Get money",
        "date": "2021-01-03T00:00:00.000Z",
        "__v": 0
    },

0 个答案:

没有答案
相关问题