首页 \ 问答 \ 检索MongoDB集合中对象数组中的查询元素(Retrieve the queried element in an object array in MongoDB collection)

检索MongoDB集合中对象数组中的查询元素(Retrieve the queried element in an object array in MongoDB collection)

我有这样的MongoDB文档

{
    "_id" : "toy_in_en_L02B",
    "modelCode" : "L02B",
    "path" : "/toy/in/en",
    "dataPath" : "/toy/in/en/L02B",
    "basicModelInformation" : {
        "name" : "News",
        "startingPrice" : "1234.0"
    },
    "grades" : {
        "grades" : [ 
            {
                "key" : "LVL001",
                "dataPath" : "/toy/in/en/L02B",
                "name" : "XE P",
                "price" : "1234.0"
            }, 
            {
                "key" : "LVL002",
                "dataPath" : "/toy/in/en/L02B",
                "name" : "XE P",
                "price" : "1234.0"
            },
            {
                "key" : "LVL003",
                "dataPath" : "/toy/in/en/L02B",
                "name" : "XE P",
                "price" : "1234.0"
            },
            {
                "key" : "LVL004",
                "dataPath" : "/toy/in/en/L02B",
                "name" : "XE P",
                "price" : "1234.0"
            }

        ]
    }
}

现在我想检索一个只有一个等级为LVL001的完整文档。 但是当我这样查询时

db.getCollection('models').find({$and: [{"dataPath" : "/nissan/in/en/L02B" }, {"grades.grades" : {$elemMatch: {"key":"LVL002"}}}]})

我得到了所有成绩。

我已经检查了线程检索查询元素但没有得到它。

我的代码使用的是Spring @Query。 任何json查询参数也都很有用。


I am having a MongoDB document like this

{
    "_id" : "toy_in_en_L02B",
    "modelCode" : "L02B",
    "path" : "/toy/in/en",
    "dataPath" : "/toy/in/en/L02B",
    "basicModelInformation" : {
        "name" : "News",
        "startingPrice" : "1234.0"
    },
    "grades" : {
        "grades" : [ 
            {
                "key" : "LVL001",
                "dataPath" : "/toy/in/en/L02B",
                "name" : "XE P",
                "price" : "1234.0"
            }, 
            {
                "key" : "LVL002",
                "dataPath" : "/toy/in/en/L02B",
                "name" : "XE P",
                "price" : "1234.0"
            },
            {
                "key" : "LVL003",
                "dataPath" : "/toy/in/en/L02B",
                "name" : "XE P",
                "price" : "1234.0"
            },
            {
                "key" : "LVL004",
                "dataPath" : "/toy/in/en/L02B",
                "name" : "XE P",
                "price" : "1234.0"
            }

        ]
    }
}

Now I want to retrieve a complete Document with having only one grade as LVL001. But when I am querying this like

db.getCollection('models').find({$and: [{"dataPath" : "/nissan/in/en/L02B" }, {"grades.grades" : {$elemMatch: {"key":"LVL002"}}}]})

I am getting all the grades.

I already checked the thread Retrieve Queried Element but not getting it.

My code is using the Spring @Query. Any json query parameter for this will also be useful.


原文:https://stackoverflow.com/questions/44656666
更新时间:2023-03-08 17:03

最满意答案

您可以尝试使用库存项目加载而不是产品加载。 请尝试使用下面的代码:

$productCollection = Mage::getResourceModel('catalog/product_collection')
                   ->addFieldToFilter(array(
                    array('attribute'=>'reference','neq'=>''),
    ));
//Update Prices and Stock
foreach ($productCollection as $product) {
 try {
    $reference = $product->getReference();
    $res = $products_api->xpath("item/sku[.='{$reference}']/parent::*"); 

    if (count($res) <= 0){
       unset($product);
       unset($res);
       unset($reference);
       continue;
    }


    //Search for Stock
    $stock = $res[0]->stocks->quantity;
    //Update Stock

    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
    if ($stockItem->getId()) {
        $isInStock = $stock > 0 ? 1 : 0;
        $stockItem->setQty($stock);
        $stockItem->setIsInStock($isInStock);
        $stockItem->setManageStock(1);
        $stockItem->setUseConfigManageStock(0);
        $stockItem->save();
    }

采取从Sonassi博客 - 大众更新Magento股票水平 - 快速


You may try to use the stock item loading instead of the product loading. Please, try to use the code below:

$productCollection = Mage::getResourceModel('catalog/product_collection')
                   ->addFieldToFilter(array(
                    array('attribute'=>'reference','neq'=>''),
    ));
//Update Prices and Stock
foreach ($productCollection as $product) {
 try {
    $reference = $product->getReference();
    $res = $products_api->xpath("item/sku[.='{$reference}']/parent::*"); 

    if (count($res) <= 0){
       unset($product);
       unset($res);
       unset($reference);
       continue;
    }


    //Search for Stock
    $stock = $res[0]->stocks->quantity;
    //Update Stock

    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
    if ($stockItem->getId()) {
        $isInStock = $stock > 0 ? 1 : 0;
        $stockItem->setQty($stock);
        $stockItem->setIsInStock($isInStock);
        $stockItem->setManageStock(1);
        $stockItem->setUseConfigManageStock(0);
        $stockItem->save();
    }

Taken from Sonassi blog - Mass update stock levels in Magento - FAST

相关问答

更多
  • 什么会更快,调用Clear()方法或创建一个新的List()? 这是不可能回答的。 这真的取决于许多因素,包括收集时间已经存在。 这里最好的选择是: 介绍应用程序,看看这是否真的很重要。 这可能不会产生任何可察觉的差异,在这种情况下,我会使用最有意义的方法,就如何看待这个对象。 如果它是重要的,写两组代码,并测量速度的差异(如果有的话)。 从实际的角度来看,调用Clear()实际上不会减少内存(由List本身使用),因为它不缩小列表的容量,只会消除其中包含的值。 创建一个新的List将导致一个新 ...
  • 如果这是最重要的查询,我建议将此索引设为CLUSTERED。 你必须在索引策略中考虑entry_date (SQL Server)创建由所有这些列组成的“索引键列”:content_definition_id,order,scope_id,entry_date并将value_id作为“包含列” if this is the most important query, I suggest to make this index the CLUSTERED. you have to consider entry_ ...
  • 你可以做两件事情。 发送'multi'标志设置为true的更新。 存储功能服务器端,并尝试使用服务器端代码执行 。 该链接还包含以下建议: 这是执行批量管理工作的好技巧。 在服务器上运行mongo,通过localhost接口进行连接。 然后连接非常快速和低延迟。 这比db.eval()更友好,因为db.eval()阻止其他操作。 这可能是你最快的。 您必须意识到,在单个服务器上发布9M更新将是一个沉重的操作。 假设你可以获得3k的更新/秒,你还在谈论运行近一个小时。 这不是一个真正的“麻将问题”,这将是一个 ...
  • 假想; 教DB数据库的概念 “你的数据库如何使用基数来优化你的查询” 所以, 基本上它们是相同的,但我会提到一个暗示“ 为什么 ”的想法,它实际上会引入一个好的RDBMS概念。 让我们假设您的RDBMS 严格按照您指定的顺序使用WHERE子句。 在这种情况下,最佳查询将是首先指定具有最大基数的列的查询。 这意味着首先指定class=5会更快,因为它会更快地消除行的考虑,这意味着如果行的“class”列不包含5(这在统计上比不包含2的“school”列更可能) ),那么它甚至不需要评估“学校”专栏。 然而,回 ...
  • 你说永远的查询是做一个完全冗余的笛卡尔连接,你在那里生成一大堆额外的行(当然行数乘以courseCatalog中的行数)然后用DISTINCT再次删除它们,留给你原始内容course表。 (与SELECT DISTINCT CourseCode, CourseName FROM course ) 要从两个表中获取不同的CourseCodes和CourseNames列表,可以使用UNION运算符。 SELECT CourseCode, CourseName FROM course UNION /*Will e ...
  • 你有没有尝试过 db.coll.update({}, {/* your changes here */}, { multi: true}) 这将更改与第一个参数匹配的所有文档,该参数为空,因此它匹配所有文档。 Have you tried db.coll.update({}, {/* your changes here */}, { multi: true}) This will change all documents that match the first parameter, which i ...
  • 您可以尝试使用库存项目加载而不是产品加载。 请尝试使用下面的代码: $productCollection = Mage::getResourceModel('catalog/product_collection') ->addFieldToFilter(array( array('attribute'=>'reference','neq'=>''), )); //Update Prices and Stock foreac ...
  • 您捕获音频(记录,输入或文件),从pcm cbr(未压缩,具有固定采样率)流访问其样本,并读取您感兴趣的范围的样本。考虑到高频率,您将只需要分析少量样本(2-5,取决于采样率)。 您可能需要进行插值以提高样本的准确性。 you capture the audio (record, input, or file), access its samples from the pcm cbr (uncompressed, with a fixed sampling rate) stream, and read th ...
  • 你可以这样做: collection2 = collection.map { |c| [c[:external_id], c.except(:external_id)]}.to_h def update ChildModel.where(external_id: collection2.keys).each |cm| do ext_id = cm.external_id cm.assign_attributes collection2[ext_id] cm.save if cm ...
  • 创建一个包含要保留的行的新表可能会更快,删除旧表,然后重命名新表 it might be quicker to create a new table containing the rows you want to keep, drop the old table and then rename the new table

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。