要进行串联,请在MongoDB中使用$concatArrays。首先让我们创建一个包含文档的集合-
>db.demo14.insertOne({"ListOfStudent":["Carol","Mike","Sam"],"ListOfTeacher":["Robert","David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e0f754bd7df943a7cec4faa") }
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.demo14.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5e0f754bd7df943a7cec4faa"), "ListOfStudent" : [ "Carol", "Mike", "Sam" ], "ListOfTeacher" : [ "Robert", "David" ] }
以下是连接数组的查询-
> db.demo14.aggregate([ ... { "$project": { ... "ListOfPeople": { "$concatArrays": [ "$ListOfStudent", "$ListOfTeacher" ] } ... }} ... ]);
这将产生以下输出-
{ "_id" : ObjectId("5e0f754bd7df943a7cec4faa"), "ListOfPeople" : [ "Carol", "Mike", "Sam", "Robert", "David" ] }