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

DynamoDB with NodeJS - Get list of elements with ID not equal first query result

我目前正在学习使用 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”的所有习惯

我可以通过对 query/params 进行一些更改来执行此操作,还是我绝对必须执行另一个将 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
}