首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带键的Mongo嵌套查询

带键的Mongo嵌套查询
EN

Stack Overflow用户
提问于 2015-09-21 15:50:37
回答 3查看 449关注 0票数 1

需要关于MongoDB嵌套查询的帮助。下面是我的芒果收藏。

偏好集合

代码语言:javascript
复制
{
    "_id" : "user123",
    "preferences" : {
        "product-1" : {
            "frequency" : "Weekly",
            "details" : {
                 "email" : {
                    "value" : "On"
                }
            }
        },
        "product-2" : {
            "preferencesFor" : "mpc-other",
            "preferencesForType" : "Product",
            "details" : {
                "email" : {
                    "value" : "Off"
                }
            }
        },
        "product-3" : {
            "preferencesFor" : "mpc-other",
            "preferencesForType" : "Product",
            "details" : {
                "email" : {
                    "value" : "On"
                }
            }
        }
    }   
}

产品集合

代码语言:javascript
复制
{
    "_id" : "product-1",
     "name" : "Geo-Magazine" 
}
{
    "_id" : "product-2",
     "name" : "History-Magazine"
}
{
    "_id" : "product-3",
     "name" : "Science-Magazine"   
}

产品1,产品2.是地图上的钥匙。密钥存储在另一个集合产品集合中。我可以创建一个嵌套查询来交叉引用另一个表中的产品密钥吗?我需要下表格式的输出。请建议一下。

user123产品-1封电子邮件 user123产品-2封电子邮件关闭 user123产品-3封电子邮件

我试了下,但没有得到结果。请建议一下。

代码语言:javascript
复制
var cursor = db.productSummary.find();   
while(cursor.hasNext()){
    var sku = cursor.next()._id;
    var skuCol = "preferences."+sku+".details.email";
    var skuVal = "preferences."+sku+".details.email.value";
 db.marketingPreferences.find( {}, {_id:1, skuCol:1, skuVal:1});     
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-09-22 18:40:20

代码语言:javascript
复制
> var myCursor = db.productSummary.find();
> while(myCursor.hasNext()){   
var sku = myCursor.next()._id;    
var skuCol = "preferences."+sku+".details.email";    
var skuVal = "$preferences."+sku+".details.email.value";    
var result = db.marketingPreferences.aggregate([{"$project":{"_id":1,value:skuVal,preferences:{$literal: sku}}}],{allowDiskUse: true});
    while(result.hasNext()){ 
        printjson(result.next());    
    }      
}

Result 

{ "_id" : "user123", "preferences" : "product-1", "value" : "On" }

{ "_id" : "user123", "preferences" : "product-2", "value" : "Off" }

{ "_id" : "user123", "preferences" : "product-3", "value" : "On" }
票数 1
EN

Stack Overflow用户

发布于 2015-09-21 18:29:09

MongoDB和普通SQL之间有一个区别。首先,当查询MongoDB集合时,它不会像在SQL中那样返回一行。这里得到的是一个类似于JSON的文档。

另外,当你使用preferences.product-1.details.email : 1时,它不会返回‘电子邮件’这个词,相反它会返回你的值。{"value" : "On" }

使用这个:db.preference.find({},{"_id":1,"preferences.product1.details.email.value":1})

您将能够获得两个细节,即user123On,并且您可以从先前的查询中获得product-1。您可以将这些值存储在变量中,并继续打印它们以获得所需的表。此外,您还需要另一个游标来存储第二个查询的结果。

如果您的查询是单个独立查询,那么它将产生什么结果:

> db.preference.find({},{"_id":1,"preferences.product1.details.email.value":1}) .pretty()

代码语言:javascript
复制
{
"_id": "user123",
"preferences": {
    "product-1": {
        "details": {
            "email": {
                "value": "On"
            }
        }
    }
}
}
票数 0
EN

Stack Overflow用户

发布于 2015-09-22 05:34:35

代码语言:javascript
复制
public static void test(){
        MongoCollection<Document> collection = getDatadase().getCollection("product");
        MongoCollection<Document> pref = getDatadase().getCollection("pref");

        List<Document> allDocList =  collection.find().into(new ArrayList<Document>());
        for(Document doc:allDocList){
            System.out.println(doc.get("_id"));
            String preferences = doc.get("_id")+"";
            String sku = "$preferences."+preferences+".details.email.value";

            Document aggregation = new Document().append("$project", new Document().append("_id", 1).append("value", sku));
            List<Document> pipeline = new ArrayList<Document>();
            pipeline.add(aggregation);

            List<Document> aggList = pref.aggregate(pipeline).into(new ArrayList<Document>());

            for(Document doc1:aggList){
                System.out.println(doc1.append("preferences", preferences));
            }
        }
    }

-这将返回产品-1

文档{{_id=user123,value=On,preferences=product-1}}

产品-2

文档{{_id=user123,value=Off,preferences=product-2}}

产品-3

文档{{_id=user123,value=On,preferences=product-3}}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32699502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档