Finding an Embedded Document by a specific property in Mongoose, Node.js, MongodDB

后端 未结 5 1667
不知归路
不知归路 2021-01-12 21:30

For this app, I\'m using Node.js, MongoDB, Mongoose & Express

So I have a Param Object that contains an array of Pivots, and I want to read certain data from the

5条回答
  •  孤独总比滥情好
    2021-01-12 22:13

    the biggest problem with this is that if your req has some fields empty (that should act as wildcard), you will not find anything since mongo tries to match empty params as well, so searching for {"user":"bob", "color":""} is not the same as {"user":"bob", "color":"red"} or {"user":"bob"}. this means that you have to first create a query object and filter out any unused parameters before you pass it in, and if you create a query object, you can no longer do something like "user.name=..." because mongo interperets this as an error since it does not first resolve the object literal into a string. Any ideas on this problem?

    ps. You'd think it would be easy enough to make an object like: user.name="bob"; user.color:"green"; user.signup.time="12342561" and then just use user as a query object :/

提交回复
热议问题