gpt4 book ai didi

mongodb - 向输出 json 添加其他属性

转载 作者:可可西里 更新时间:2023-11-01 10:40:07 26 4
gpt4 key购买 nike

有一个User的集合

{    "_id" : ObjectId("5a0d45ca8af3a91847b7cf95"),    "updatedAt" : ISODate("2017-11-16T09:34:14.651Z"),    "createdAt" : ISODate("2017-11-16T08:01:14.119Z"),    "name" : "John",    "email" : "test1@gmail.com",    "groupsFavorite" : [         ObjectId("5a0d45db8af3a91847b7cf96")    ],    "groups" : [         ObjectId("5a0d45db8af3a91847b7cf96"),         ObjectId("5a0d45e18af3a91847b7cf97")    ],    "__v" : 3}

有一组集合

/* 1 */{    "_id" : ObjectId("5a0d45db8af3a91847b7cf96"),    "updatedAt" : ISODate("2017-11-16T08:01:31.815Z"),    "createdAt" : ISODate("2017-11-16T08:01:31.815Z"),    "userId" : ObjectId("5a0d45ca8af3a91847b7cf95"),    "title" : "New title",    "slug" : "new-title-1",    "description" : "Lorem",    "__v" : 0}/* 2 */{    "_id" : ObjectId("5a0d45e18af3a91847b7cf97"),    "updatedAt" : ISODate("2017-11-16T08:01:37.005Z"),    "createdAt" : ISODate("2017-11-16T08:01:37.005Z"),    "userId" : ObjectId("5a0d45ca8af3a91847b7cf95"),    "title" : "New title",    "slug" : "new-title-2",    "description" : "Lorem",    "__v" : 0}/* 3 */{    "_id" : ObjectId("5a0d5cb0cd59342da943d55a"),    "updatedAt" : ISODate("2017-11-16T09:38:56.912Z"),    "createdAt" : ISODate("2017-11-16T09:38:56.912Z"),    "userId" : ObjectId("5a0d5c48cd59342da943d559"),    "title" : "New title",    "slug" : "new-title-3",    "description" : "Lorem",    "__v" : 0}

有一种方法可以返回特定用户的组。有必要将这些数据添加到favorite: truefalse 的新属性中以建表。

例如:

{    "_id" :"5a0d45db8af3a91847b7cf96",    "updatedAt" : "2017-11-16T08:01:31.815Z",    "createdAt" : "2017-11-16T08:01:31.815Z",    "userId" : "5a0d45ca8af3a91847b7cf95",    "title" : "New title",    "slug" : "new-title-1",    "description" : "Lorem",    "favorite": "true"},{    "_id" : "5a0d45e18af3a91847b7cf97",    "updatedAt" : "2017-11-16T08:01:37.005Z",    "createdAt" : "2017-11-16T08:01:37.005Z",    "userId" : "5a0d45ca8af3a91847b7cf95",    "title" : "New title",    "slug" : "new-title-2",    "description" : "Lorem",    "favorite": "false"},{    "_id" : "5a0d5cb0cd59342da943d55a",    "updatedAt" : "2017-11-16T09:38:56.912Z",    "createdAt" : "2017-11-16T09:38:56.912Z",    "userId" : "5a0d5c48cd59342da943d559",    "title" : "New title",    "slug" : "new-title-3",    "description" : "Lorem",    "favorite": "false"}

最佳答案

您可以使用 $lookup 运算符来检查“用户”集合中的 _iduserId 字段。并且为了确定它是否存在,您可以使用 $eq 运算符。

db.getCollection('Groups').aggregate([
{
$lookup:
{
from: "User",
localField: "_id",
foreignField: "groupsFavorite",
as: "FavoriteByGrp"
}
}
,{
$lookup:
{
from: "User",
localField: "userId",
foreignField: "_id",
as: "FavoriteByUsr"
}
}
,{
"$project":
{
_id:1,
updatedAt:1,
createdAt:1,
userId:1,
title:1,
slug:1,
description:1,
favorite:
{
"$cond":
{
if: { "$eq": [ "$FavoriteByGrp._id", "$FavoriteByUsr._id" ] },
then: "true",
else: "false"
}
}
}

}
])

结果:

/* 1 */
{
"_id" : ObjectId("5a0d45db8af3a91847b7cf96"),
"updatedAt" : ISODate("2017-11-16T08:01:31.815Z"),
"createdAt" : ISODate("2017-11-16T08:01:31.815Z"),
"userId" : ObjectId("5a0d45ca8af3a91847b7cf95"),
"title" : "New title",
"slug" : "new-title-1",
"description" : "Lorem",
"favorite" : "true"
}

/* 2 */
{
"_id" : ObjectId("5a0d45e18af3a91847b7cf97"),
"updatedAt" : ISODate("2017-11-16T08:01:37.005Z"),
"createdAt" : ISODate("2017-11-16T08:01:37.005Z"),
"userId" : ObjectId("5a0d45ca8af3a91847b7cf95"),
"title" : "New title",
"slug" : "new-title-2",
"description" : "Lorem",
"favorite" : "false"
}

/* 3 */
{
"_id" : ObjectId("5a0d5cb0cd59342da943d55a"),
"updatedAt" : ISODate("2017-11-16T09:38:56.912Z"),
"createdAt" : ISODate("2017-11-16T09:38:56.912Z"),
"userId" : ObjectId("5a0d5c48cd59342da943d559"),
"title" : "New title",
"slug" : "new-title-3",
"description" : "Lorem",
"favorite" : "false"
}

关于mongodb - 向输出 json 添加其他属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47330806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com