Arrays 在MongoDB中选择特定的索引字段

Arrays 在MongoDB中选择特定的索引字段,arrays,json,mongodb,Arrays,Json,Mongodb,我只想选择特定索引上的“thetext”字段,例如当“comment\u count”=1 我尝试了以下查询: db.getCollection('mongotesi').find({},{'bug.long_desc.1.thetext':'1}) 我还想显示所有的“thetext”字段。 这是json结构: { "_id" : ObjectId("5613c8acc8e53ab811000083"), "@attributes" : { "vers

我只想选择特定索引上的“thetext”字段,例如当
“comment\u count”=1

我尝试了以下查询:

db.getCollection('mongotesi').find({},{'bug.long_desc.1.thetext':'1})

我还想显示所有的“thetext”字段。 这是json结构:

    { 
    "_id" : ObjectId("5613c8acc8e53ab811000083"), 
    "@attributes" : {
        "version" : "4.4.10", 
        "urlbase" : "https://bugs.documentfoundation.org/", 
        "maintainer" : "hostmaster@documentfoundation.org"
    }, 
    "bug" : {
        "bug_id" : "31585", 
        "creation_ts" : "2010-11-12 08:55:00 +0000", 
        "long_desc" : [
            {
                "@attributes" : {
                    "isprivate" : "0"
                }, 
                "commentid" : "194230", 
                "comment_count" : "0", 
                "attachid" : "40242", 
                "who" : "eric.moret", 
                "bug_when" : "2010-11-12 08:55:52 +0000", 
                "thetext" : "Created attachment 40242ooo base fileI am running ooo 3.2.1 OOO320m18 (Build:9502). I am using an odb bas file to perform a mail merge on an odt file under writer. In the mail merge Wizard, on hitting Next in step 6. (Edit Document), the Status window - creating document opens up and generates my mailing. The crash happens reliably while performing this task. I have 118 items to generate.My error report id is: rpmrd6nAttached are the 2 files used for this merge."
            }, 
            {
                "@attributes" : {
                    "isprivate" : "0"
                }, 
                "commentid" : "194232", 
                "comment_count" : "1", 
                "attachid" : "40243", 
                "who" : "eric.moret", 
                "bug_when" : "2010-11-12 08:56:29 +0000", 
                "thetext" : "Created attachment 40243ooo write file"
            },    
        ], 
    }
}

Mongo不会这样做,但这会起作用:

var tm = require('tree-math');
var long_desc=db.getCollection('mongotesi').find({},{'bug.long_desc':'1'});
var bugtext = {};
tm.find(long_desc,function(path,val {if(path[3]==='thetext')tm.setPath(bugtext,path,val);});
console.log(JSON.stringify(bugtext,null,2));
数组采用[skip,limit]的形式,其中第一个值表示数组中要跳过的项数,第二个值表示要返回的项数

使用查询将用于查找第一个数组对象

db.collection.find({},{"bug.long_desc":{"$slice":[0,1]},"@attributes":0})
若你们发现了第二个对象,那个么像这样跳过第一个对象

db.collection.find({},{"bug.long_desc":{"$slice":[1,2]},"@attributes":0})
编辑

如果打印完全匹配的文本,则用作:


我昨天问了一些非常类似的问题,答案是不,mongo还不支持这个。如果你愿意,我可以为你提供参考资料和工具。我的问题是:我使用节点模块“树数学”在数据出来时对数据进行切片和切分。免责声明:我写了树数学。升级:树数学非常棒。不是返回精确的键值,而是使用Whit$slice返回特定的位置数组。我的结果总是从0位置开始。如果我想要n位置,如何使用$slice?你能帮我写一个问题吗?谢谢,但我用的是R语言,建议?好的,非常感谢。现在,如果我只想打印“thetext”,我该怎么办
db.mongotesi.findOne({},{“bug.long_desc”:{$slice:[0,1]}).bug.long_desc
next.bug.long_desc“我必须添加什么?”@J.arc检查编辑的答案以打印精确匹配的键,然后使用forEach进行查询
db.collection.find({},{"bug.long_desc":{"$slice":[0,1]},"@attributes":0}).forEach(function(doc){print(doc.bug.long_desc[0].thetext);})