Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

mongodb - How to use find where query in Mongoose

I've been writing a lot of MySQL lately so I've forgotten how to write some Mongoose queries. I am trying to search my database to find stocks where the user is the same as the req.params.user. below is the route and my Schema so you can see what's going on. I'm running it in Post man, but it's giving me a "Cannot GET /stock/user-stocks/Sina" Error. I also left a sample stock in my DB for reference

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
    },
question from:https://stackoverflow.com/questions/65557325/how-to-use-find-where-query-in-mongoose

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...