检索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
最满意答案
您可以尝试使用库存项目加载而不是产品加载。 请尝试使用下面的代码:
$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_ ...
-
MongoDB - 更新集合中所有记录的最快方法是什么?(MongoDB - what is the fastest way to update all records in a collection?)[2022-05-06]
你可以做两件事情。 发送'multi'标志设置为true的更新。 存储功能服务器端,并尝试使用服务器端代码执行 。 该链接还包含以下建议: 这是执行批量管理工作的好技巧。 在服务器上运行mongo,通过localhost接口进行连接。 然后连接非常快速和低延迟。 这比db.eval()更友好,因为db.eval()阻止其他操作。 这可能是你最快的。 您必须意识到,在单个服务器上发布9M更新将是一个沉重的操作。 假设你可以获得3k的更新/秒,你还在谈论运行近一个小时。 这不是一个真正的“麻将问题”,这将是一个 ... -
哪个sql会更快,为什么?(Which sql will be faster and why?)[2022-12-11]
假想; 教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 ...
-
更快地更新收集记录(Updating collection records faster)[2023-08-13]
您可以尝试使用库存项目加载而不是产品加载。 请尝试使用下面的代码: $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