对于数组串联,请使用$concatArrays运算符。首先让我们创建一个包含文档的集合-
>db.arrayConcatenationDemo.insertOne({"TeacherName":["Chris","Robert"],"StudentName":["Mike","Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ce921c078f00858fb12e911") }
以下是在find()方法的帮助下显示集合中所有文档的查询-
> db.arrayConcatenationDemo.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5ce921c078f00858fb12e911"), "TeacherName" : [ "Chris", "Robert" ], "StudentName" : [ "Mike", "Sam" ] }
以下是数组连接的查询-
> db.arrayConcatenationDemo.aggregate([ { "$project": { "StudentAndTeacherName": { "$concatArrays": [ "$TeacherName", "$StudentName" ] } }} ]);
这将产生以下输出-
{ "_id" : ObjectId("5ce921c078f00858fb12e911"), "StudentAndTeacherName" : [ "Chris", "Robert", "Mike", "Sam" ] }