Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 阵列内文档上的$Filter和mongoDB_Node.js_Mongodb_Mongodb Query_Handlebars.js - Fatal编程技术网

Node.js 阵列内文档上的$Filter和mongoDB

Node.js 阵列内文档上的$Filter和mongoDB,node.js,mongodb,mongodb-query,handlebars.js,Node.js,Mongodb,Mongodb Query,Handlebars.js,我试图构建一个查询来从我的mongoDB获取信息,但我需要过滤结果并只获取相应的值 这是我一直尝试到现在的方式。我需要匹配expensesHouse.\u id与我将在链接中传递的id router.get("/showExpense/:id", ensureAuthenticated, (req, res) => { House.aggregate([ { expensesHouse: { $filter: { input: "

我试图构建一个查询来从我的mongoDB获取信息,但我需要过滤结果并只获取相应的值

这是我一直尝试到现在的方式。我需要匹配expensesHouse.\u id与我将在链接中传递的id

router.get("/showExpense/:id", ensureAuthenticated, (req, res) => {
  House.aggregate([
    {
      expensesHouse: {
        $filter: {
          input: "$expensesHouse",
          as: "each",
          cond: { $eq: ["$$each._id", req.params.id] }
        }
      }
    }
  ]).then(house => {
    console.log(house);
    res.render("houses/showExpense", {
      house: house
    });
  });
下面的屏幕是mongodb模式。 我需要从expensesHouse获取每个值,但只有通过的“id”需要与expensesHouse.id匹配

结果出来后,我需要在我的车把上使用它们

<form>
                    {{#each house}}
                    {{#each expensesHouse}}
                    <div class="form-group">
                        <label for="expenseType" class="text-dark">Expense Type</label>
                        <input type="text" class="form-control" name="expenseType" value="{{expenseType}}" disabled>
                    </div>
                    <div class="form-group">
                        <label for="description" class="text-dark">Description</label>
                        <input type="text" class="form-control" name="description" value="{{description}}" disabled>
                    </div>
                    <div class="form-group">
                        <label for="price" class="text-dark">Price</label>
                        <input type="text" class="form-control" name="price" value="{{price}}" disabled>
                    </div>

                    <div class="form-group">
                        <label for="status" class="text-dark">Price</label>
                        <input type="text" class="form-control" name="status" value="{{status}}" disabled>
                    </div>
                    {{/each}}
                    {{/each}}
                </form>

{{{每家每户}
{{{#每项费用}
费用类型
描述
价格
价格
{{/每个}}
{{/每个}}

根据您所说的,您正在尝试实现运营商为您提供的功能

请尝试此代码,如果有帮助,请在下面进行注释:

.
. // All the other stuff
.
expensesHouse: {
    $elemMatch: {
        _id: req.params.id // or ObjectId(req.params.id)
    }
}
尝试以下选项:

1) 使用:

2) 使用:


$elemMatch从数组中获取所有值,而不是特定的值..elemMatch实际上不是这样工作的,问题是您使用的是objectId,不是吗?如果需要,您需要使用
\u id:ObjectId(req.params.id)
。告诉我这种方法是否有效我在这里测试过并且有效,我甚至更新了答案以支持这种方法我用$elemMatch:_id:ObjectId(req.params.id)更新了我的查询和搜索但是它仍然检索所有的费用,并且不仅仅是特定的id
expensesHouse
需要包装在一个
$project
stage中,我得到这个结果:[{id:5e2dea5f71cbdd1a6dd1c8fc,expensesHouse:[]},{id:5e2debbf30b9cd1aa5aca711,expensesHouse:[]},{id:5e2f57ad63e438bc44f9a,expensesHouse:[]},{{u id:5e2f5b3163e46338bc44f9c,expensesHouse:[]}]这是我所有的房子,但没有给我正确的费用。我想你应该使用
ObjectId
对象,而不是传递
字符串
var mongoose = require('mongoose');

router.get("/showExpense/:id", ensureAuthenticated, (req, res) => {
    var id = mongoose.Types.ObjectId(req.params.id);
    House.aggregate([
        {
            $addFields:
            {
                expensesHouse: {
                    $filter: {
                        input: "$expensesHouse",
                        as: "each",
                        cond: { $eq: ["$$each._id", id] }
                    }
                }
            }
        }
    ]).lean().then(house => {
        console.log(house);
        res.render("houses/showExpense", {
            house: house
        });
    })
})
    var mongoose = require('mongoose');

router.get("/showExpense/:id", ensureAuthenticated, (req, res) => {
    var id = mongoose.Types.ObjectId(req.params.id);
    House.find({ 'expensesHouse._id': id }, {
        members: 1, name: 1, description: 1, address: 1,
        type: 1, user: 1, userID: 1, userType: 1, expensesHouse: { $elemMatch: { _id: id } }, date: 1
    }).then(house => {
        console.log(house);
        res.render("houses/showExpense", {
            house: house
        });
    })
})