带有 NodeJS 的 DynamoDB - 获取 ID 不等于第一个查询结果的元素列表



我目前正在学习使用 NodeJS 的 DynamoDB。我正在做一个具有参数"userId"的函数。

我将查询dynamoDB并使用参数"userId"获取此用户,这有效。

这是我获取用户的查询:

let params = {
    TableName: 'user',
    KeyConditionExpression: "#id = :userId",
    ExpressionAttributeNames:{
        "#id": "id"
    },
    ExpressionAttributeValues: {
        ":userId": userId
    }
};
docClient.query(params, function (err, data) {
    if (err) {
        res.send({
            success: false,
            message: err.message
        });
    } else {
        const {Items} = data;
        if (Items.length === 0) {
            res.send(null)
        }
        else res.send(Items);
    }
});

这是找到用户时的查询结果:

[{
    "firstname": "test",
    "lastname": "test",
    "password": "test",
    "id": "1750",
    "email": "test@test.com",
    "habitsList": [
        {
           id: 10
        },
        {
           id: 12
        }
    ]
}]

因此,在dynamo找到用户之后,我希望它获得ID不等于"10"和"12"的所有习惯

可以通过对查询/参数进行一些更改来执行此操作,还是我绝对必须执行另一个查询,将 IDS 列表作为参数传递?

谢谢

如果我

正确理解了您的问题,这意味着您想要用户对象,但习惯被过滤为不是id = 10或12。如果是这种情况,我认为在查询后过滤习惯会更容易。

for(var i = 0; i < Items.length; i++){
    var filteredHabits = Items[i].habitsList.filter(h => h.id !== 10 && h.id !== 12);
    var Items[i].habitsList = filteredHabits; //this will replace the current habitList to filteredHabit
}

相关内容

最新更新