db.absences.insert([
{ "_id" : 1, "student" : "Ann Aardvark", sickdays: [ new Date ("2018-05-01"),new Date ("2018-08-23") ] },
{ "_id" : 2, "student" : "Zoe Zebra", sickdays: [ new Date ("2018-02-01"),new Date ("2018-05-23") ] },
])
db.holidays.insert([
{ "_id" : 1, year: 2018, name: "New Years", date: new Date("2018-01-01") },
{ "_id" : 2, year: 2018, name: "Pi Day", date: new Date("2018-03-14") },
{ "_id" : 3, year: 2018, name: "Ice Cream Day", date: new Date("2018-07-15") },
{ "_id" : 4, year: 2017, name: "New Years", date: new Date("2017-01-01") },
{ "_id" : 5, year: 2017, name: "Ice Cream Day", date: new Date("2017-07-16") }
])
db.absences.aggregate([
{
$lookup:
{
from: "holidays",
pipeline: [
{ $match: { year: 2018 } },
{ $project: { _id: 0, date: { name: "$name", date: "$date" } } },
{ $replaceRoot: { newRoot: "$date" } }
],
as: "holidays"
}
}
])
集計クエリのルックアップでパイプラインを使用しようとしています。 Mongodbのドキュメントと同じですが、それでもエラーが発生します
Unable to execute the selected commands
Mongo Server error (MongoCommandException): Command failed with error 4570: 'arguments to $lookup must be strings, pipeline: [ { $match: { year: 2018.0 } }, { $project: { _id: 0.0, date: { name: "$name", date: "$date" } } }, { $replaceRoot: { newRoot: "$date" } } ] is type array' on server localhost:27017.
The full response is:
{
"ok" : 0.0,
"errmsg" : "arguments to $lookup must be strings, pipeline: [ { $match: { year: 2018.0 } }, { $project: { _id: 0.0, date: { name: \"$name\", date: \"$date\" } } }, { $replaceRoot: { newRoot: \"$date\" } } ] is type array",
"code" : NumberInt(4570),
"codeName" : "Location4570"
}
私はmongodb v3.4を使用しています。
$lookup
機能(構文)MongoDB v3.6のMongoDB v3.4から
MongoDB v3.4 $lookup
構文:
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}
MongoDB v3.6 $lookup
構文:
{
$lookup:
{
from: <collection to join>,
let: { <var_1>: <expression>, …, <var_n>: <expression> },
pipeline: [ <pipeline to execute on the collection to join> ],
as: <output array field>
}
}
https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/