我有两本书籍和作者。当我在这些集合之间进行查找时,我得到了所需的结果,但我需要重命名" _id"到" id"。当我重命名这些字段时,作者" _id"正在被书籍取代" _id"而不是作者" _id"。请看下面的
Error in ets(object, lambda = lambda, allow.multiplicative.trend = allow.multiplicative.trend, :
y should be a univariate time series
Additional: Warnings:
1: In ets(x, "ANN", alpha = alpha, opt.crit = "mse", lambda = lambda, :
Missing values encountered. Using longest contiguous portion of time series
2: In fit$call <- match.call() : ...
结果:
Book.collection.aggregate([
{'$lookup' => {'from' => "authors",'
localField' => "_id",
'foreignField' => "book_id",
'as' => "authors"}}
])
我尝试将_id字段重命名为id
{
"_id": 1,
"title": "abc123",
"isbn": "0001122223334",
"copies": 5,
"updated_at": "2018-03-02T09:17:24.546Z",
"created_at": "2018-03-02T09:17:24.546Z",
"authors": [
{
"_id": 10,
"first": "a",
"last": "a",
"book_id": 1,
"updated_at": "2018-03-02T09:22:07.115Z",
"created_at": "2018-03-02T09:22:07.115Z"
}
]
}
在上述&#34;项目&#34;如果我说
Book.collection.aggregate([
{'$lookup' => {'from' => "authors",
'localField' => "_id",
'foreignField' => "book_id",
'as' => "authors"}},
{'$project' => {'id' => '$_id', '_id' => 0, 'title' => 1, "isbn" => 1, "copies" => 1, "updated_at" => 1,
"authors" => { 'id' => '$_id', 'first' => 1, 'last' => 1, 'book_id' => 1, 'updated_at' => 1}}}
])
然后结果是
"authors" => { 'id' => '$_id'
作者的身份是&#34; 1&#34;,而那应该是&#34; 10&#34;。 请建议我如何进行更改
答案 0 :(得分:1)
试试这个,只需展开并在项目管道中提供此if( isset($_GET['action']) )
{
$action= $_GET['action'];
// $action=''; //Just remove this code
}
$authors._id
答案 1 :(得分:0)
您可以$unwind作者,项目ID,然后分组,或使用$map单独重塑作者。我相信后者应该表现得更好,但是当你硬编码作者的字段时,它的灵活性会稍微提高一些:
Book.collection.aggregate([
{$addFields: {id:"$_id", "authors": {$map:{
input: "$authors",
as: "a",
in: {
"id": "$$a._id",
"first": "$$a.first",
"last": "$$a.last",
"book_id": "$$a.book_id",
"updated_at": "$$a.updated_at",
"created_at": "$$a.created_at"
}
}}}},
{$project: {_id:0}}
])