Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用$in仅获取一个子文档_Python_Mongodb_Search_Pymongo - Fatal编程技术网

Python 如何使用$in仅获取一个子文档

Python 如何使用$in仅获取一个子文档,python,mongodb,search,pymongo,Python,Mongodb,Search,Pymongo,如何获得正确的子文档?因为我得到了“全部” 我为这些标签感到抱歉,我只是想添加新元素,我得到了这个:(使用限制) db.users.find({"produit_up.tags":{"$in":["ddsfdsf"]}}).limit(1); 如果每个文档只有一个匹配的子文档(或者您愿意只返回第一个匹配的子文档),则可以使用 在mongoshell中使用此命令的示例: find({“produit_up.tags”:{$in:[“ddsfdsf”]},{“produit_up.$”:1}) 如

如何获得正确的子文档?因为我得到了“全部”

我为这些标签感到抱歉,我只是想添加新元素,我得到了这个:(

使用限制)

db.users.find({"produit_up.tags":{"$in":["ddsfdsf"]}}).limit(1);

如果每个文档只有一个匹配的子文档(或者您愿意只返回第一个匹配的子文档),则可以使用

mongo
shell中使用此命令的示例:

find({“produit_up.tags”:{$in:[“ddsfdsf”]},{“produit_up.$”:1})

如果要为每个文档返回多个匹配的子文档,可以使用MongoDB 2.2中的新版本:

db.users.aggregate(

    // Match on indexed `tags` field to limit results
    { $match : { "produit_up.tags":"ddsfdsf" }},

    // Convert produit_up array embedded docs to a stream of documents  
    { $unwind: "$produit_up" },

    // Find all matching subdocuments
    { $match : { "produit_up.tags":"ddsfdsf" }}
)

它不起作用,限制只针对文档(以释放内存),而不针对子文档。find_one(python版本)也会返回根文档,而不是子文档。谢谢你;D!这是python中的等效项(你只需在注释中添加-dont display,这是“符号-没有的地方”)db.users.find({)produit_up.tags:{“$in”:[“ddsfdsf”]},{“produit_up.$”:1})顺便说一句,这里的“1”表示匹配了多少子文档?所以100表示它提供了100个子文档。
{“produit_up.$”:1}
指的是将该子文档包含在结果中。使用投影操作符,它将只匹配每个文档的第一个子文档。如果您想要多个匹配的子文档,则需要使用聚合方法,如聚合框架或映射/减少。不知道这是否是一个错误,但似乎是接受任何整数,尝试了这个方法,它工作了:list(db.users.find({“produit\u up.tags”:{“$in”:[“ddsfdsf”]},{“produit\u up.$”:27}))@AbdelouahabPp:从技术上讲,字段包含是检查真值或假值。您可以使用计算结果为真的不同整数或字符串,但它不会更改返回的结果数。在Python中,使用
True
常量比使用
1
更为清晰。
{
    "_id" : ObjectId("5055a7abac2ec70755816f7b"),
    "produit_up" : [
        {
            "abus" : 0,
            "avctype" : "image/jpeg",
            "date" : "2012-09-15",
            "description" : "ddsfdsf sdfsdfsdf",
            "id" : "alucaard134773657029",
            "namep" : "nokia 3310",
            "nombre" : 2,
            "orientation" : "portrait",
            "photo" : ObjectId("5054d3fa3a5f3a0598b792a2"),
            "prix" : 24,
            "tags" : [
                "ddsfdsf",
                "sdfsdfsdf"
            ],
            "vend" : false
        }
    ]
}
db.users.aggregate(

    // Match on indexed `tags` field to limit results
    { $match : { "produit_up.tags":"ddsfdsf" }},

    // Convert produit_up array embedded docs to a stream of documents  
    { $unwind: "$produit_up" },

    // Find all matching subdocuments
    { $match : { "produit_up.tags":"ddsfdsf" }}
)