gpt4 book ai didi

java - $unwind $push Mongo

转载 作者:行者123 更新时间:2023-12-01 14:12:05 27 4
gpt4 key购买 nike

我有一份教育机构文件,看起来像这样:

{ name: ..., addresses: [...], courses: [ {name: ... , duration: ..., tags[...]} ] }

标签有一个字符串数组。

我正在尝试找到一门包含一些标签的类(class),例如:java、eclipse、struts 等...

我的搜索方法如下所示:

public BasicDBList coordinates(List tags){


 BasicDBObject cmdBody = new BasicDBObject("aggregate", "EducationalInstitution");

List<BasicDBObject> pipeline = new ArrayList<BasicDBObject>();

BasicDBObject projectParams = new BasicDBObject();
projectParams.put("name", 1);
projectParams.put("addresses.state", 1);
projectParams.put("addresses.locs", 1);
projectParams.put("courses", 1);

pipeline.add(new BasicDBObject("$project", projectParams));
pipeline.add(new BasicDBObject("$unwind", "$addresses"));
pipeline.add(new BasicDBObject("$unwind", "$courses"));
pipeline.add(new BasicDBObject("$match", new BasicDBObject("courses.tags", new BasicDBObject("$all", tags))));

cmdBody.put("pipeline", pipeline);

return (BasicDBList) getDatastore().getDB().command(cmdBody).get("result");
}

当我运行它时,我收到如下结果:

{ "_id" : ... , "name" : "X25" , "addresses" : { "state" : "DF" , "locs" : [ -15.806789 , -47.912779]} , "courses" : { "name" : "Microsoft Office Word 2010" , "duration" : 22 , "tags" : [...]}}


{ "_id" : ... , "name" : "X25" , "addresses" : { "state" : "DF" , "locs" : [ -15.806789 , -47.912779]} , "courses" : { "name" : "Microsoft Office Excel 2010" , "duration" : 18 , "tags" : [...]}}

{ "_id" : ... , "name" : "X25" , "addresses" : { "state" : "DF" , "locs" : [ -15.806789 , -47.912779]} , "courses" : { "name" : "Microsoft Office PowerPoint 2010" , "duration" : 14 , "tags" : [...]}}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "DF" , "locs" : [ -15.797209 , -47.883596]} , "courses" : { "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "DF" , "locs" : [ -15.797209 , -47.883596]} , "courses" : { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "DF" , "locs" : [ -15.797209 , -47.883596]} , "courses" : { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "PR" , "locs" : [ -25.431803 , -49.279532]} , "courses" : { "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "PR" , "locs" : [ -25.431803 , -49.279532]} , "courses" : { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "PR" , "locs" : [ -25.431803 , -49.279532]} , "courses" : { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "SP" , "locs" : [ -23.574942 , -46.71048]} , "courses" : { "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "SP" , "locs" : [ -23.574942 , -46.71048]} , "courses" : { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "SP" , "locs" : [ -23.574942 , -46.71048]} , "courses" : { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}}

我想要的是我根据教育机构名称和地址放松的小组类(class)。像这样:
{ "_id" : ... , "name" : "X25" , "addresses" : { "state" : "DF" , "locs" : [ -15.806789 , -47.912779]} , "courses" : [{ "name" : "Microsoft Office Word 2010" , "duration" : 22 , "tags" : [...]}, { "name" : "Microsoft Office Excel 2010" , "duration" : 18 , "tags" : [...]}, { "name" : "Microsoft Office PowerPoint 2010" , "duration" : 14 , "tags" : [...]}]}


{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "DF" , "locs" : [ -15.797209 , -47.883596]} , "courses" : [{ "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}, { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}, { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}]}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "PR" , "locs" : [ -25.431803 , -49.279532]} , "courses" : [{ "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}, { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}, { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}]}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "SP" , "locs" : [ -23.574942 , -46.71048]} , "courses" : [{ "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}, { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}, { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}]}

我阅读了 $push 并尝试实现,但没有成功。我尝试在管道变量中添加 BasicDBObject 并在 cmdBody 中追加命令。

有人通过了类似的问题吗?

最佳答案

您可以在 shell 中使用 $push 运算符和 $group 管道运算符,如下所示:

db.t.aggregate([{$unwind:'$b'},{$unwind:'$c'},{$group:{_id:'$b',cs:{$push:'$c'}}}])

在您的 JAVA 情况下,例如:
pipeline.add(new BasicDBObject("$group", new BasicDBObject(new BasicDBObject("_id", groupParams)).append("courses", new BasicDBObject("$push", "$courses"))));

如果您非常有可能使用与您描述的格式相同的格式,则可以在最后包含一个 $project 步骤,您可以重新格式化结果文档。

关于java - $unwind $push Mongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18435066/

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