gpt4 book ai didi

mongodb - 匹配数组中的多个值

转载 作者:行者123 更新时间:2023-12-02 20:27:18 26 4
gpt4 key购买 nike

我有以下数据结构:

[{"url":"http://guyanachronicle.com/","originalUrl":"http://www.guyanachronicle.com","applications":[]}
,{"url":"http://www.lightbot.com","originalUrl":"http://www.lightbot.com","applications":[{"name":"Apache","confidence":"100","version":"","icon":"Apache.svg","website":"http://apache.org","categories":["Web Servers"]},{"name":"Facebook","confidence":"100","version":"","icon":"Facebook.svg","website":"http://facebook.com","categories":["Widgets"]},{"name":"Google Analytics","confidence":"100","version":"","icon":"Google Analytics.svg","website":"http://google.com/analytics","categories":["Analytics"]},{"name":"Twitter","confidence":"100","version":"","icon":"Twitter.svg","website":"http://twitter.com","categories":["Widgets"]},{"name":"jQuery","confidence":"100","version":"1.4","icon":"jQuery.svg","website":"http://jquery.com","categories":["JavaScript Frameworks"]}]}
,{"url":"http://www.fitnessfirst.com.au","originalUrl":"http://www.fitnessfirst.com.au","applications":[{"name":"AdInfinity","confidence":"100","version":"","icon":"AdInfinity.png","website":"http://adinfinity.com.au","categories":["Advertising Networks"]},{"name":"CloudFlare","confidence":"100","version":"","icon":"CloudFlare.svg","website":"http://www.cloudflare.com","categories":["CDN"]},{"name":"Facebook","confidence":"100","version":"","icon":"Facebook.svg","website":"http://facebook.com","categories":["Widgets"]},{"name":"Google Analytics","confidence":"100","version":"UA","icon":"Google Analytics.svg","website":"http://google.com/analytics","categories":["Analytics"]},{"name":"Google Tag Manager","confidence":"100","version":"","icon":"Google Tag Manager.png","website":"http://www.google.com/tagmanager","categories":["Tag Managers"]},{"name":"Microsoft ASP.NET","confidence":"100","version":"4.0.30319","icon":"Microsoft ASP.NET.png","website":"http://www.asp.net","categories":["Web Frameworks"]},{"name":"Modernizr","confidence":"100","version":"","icon":"Modernizr.png","website":"http://www.modernizr.com","categories":["JavaScript Frameworks"]},{"name":"New Relic","confidence":"100","version":"","icon":"New Relic.png","website":"http://newrelic.com","categories":["Analytics"]},{"name":"Nginx","confidence":"100","version":"","icon":"Nginx.svg","website":"http://nginx.org/en","categories":["Web Servers"]},{"name":"OWL Carousel","confidence":"100","version":"","icon":"OWL Carousel.png","website":"https://owlcarousel2.github.io/OwlCarousel2/","categories":["Widgets"]},{"name":"Optimizely","confidence":"100","version":"","icon":"Optimizely.png","website":"http://optimizely.com","categories":["Analytics"]},{"name":"YouTube","confidence":"100","version":"","icon":"YouTube.png","website":"http://www.youtube.com","categories":["Video Players"]},{"name":"jQuery","confidence":"100","version":"1.11.1","icon":"jQuery.svg","website":"http://jquery.com","categories":["JavaScript Frameworks"]},{"name":"IIS","confidence":"100","version":"","icon":"IIS.png","website":"http://www.iis.net","categories":["Web Servers"]},{"name":"Windows Server","confidence":"100","version":"","icon":"Microsoft.svg","website":"http://microsoft.com/windowsserver","categories":["Operating Systems"]}]}
,{"url":"https://www.totaltools.com.au/","originalUrl":"http://www.totaltools.com.au","applications":[{"name":"Facebook","confidence":"100","version":"","icon":"Facebook.svg","website":"http://facebook.com","categories":["Widgets"]},{"name":"Google Analytics","confidence":"100","version":"UA","icon":"Google Analytics.svg","website":"http://google.com/analytics","categories":["Analytics"]},{"name":"Google Tag Manager","confidence":"100","version":"","icon":"Google Tag Manager.png","website":"http://www.google.com/tagmanager","categories":["Tag Managers"]},{"name":"Hotjar","confidence":"100","version":"","icon":"Hotjar.png","website":"https://www.hotjar.com","categories":["Analytics"]},{"name":"Magento","confidence":"100","version":"2","icon":"Magento.png","website":"http://www.magentocommerce.com","categories":["Ecommerce"]},{"name":"Prototype","confidence":"100","version":"","icon":"Prototype.png","website":"http://www.prototypejs.org","categories":["JavaScript Frameworks"]},{"name":"RequireJS","confidence":"100","version":"","icon":"RequireJS.png","website":"http://requirejs.org","categories":["JavaScript Frameworks"]},{"name":"Twitter Bootstrap","confidence":"100","version":"","icon":"Twitter Bootstrap.png","website":"http://getbootstrap.com","categories":["Web Frameworks"]},{"name":"Underscore.js","confidence":"100","version":"","icon":"Underscore.js.png","website":"http://underscorejs.org","categories":["JavaScript Frameworks"]},{"name":"jQuery","confidence":"100","version":"","icon":"jQuery.svg","website":"http://jquery.com","categories":["JavaScript Frameworks"]},{"name":"jQuery Mobile","confidence":"100","version":"","icon":"jQuery Mobile.svg","website":"http://jquerymobile.com","categories":["Mobile Frameworks"]},{"name":"jQuery UI","confidence":"100","version":"","icon":"jQuery UI.svg","website":"http://jqueryui.com","categories":["JavaScript Frameworks"]},{"name":"PHP","confidence":"100","version":"","icon":"PHP.svg","website":"http://php.net","categories":["Programming Languages"]}]}
,{"url":"http://www.bestandless.com.au","originalUrl":"http://www.bestandless.com.au","applications":[]}
,{"url":"http://www.flightsimstore.com","originalUrl":"http://www.flightsimstore.com","applications":[{"name":"Google Analytics","confidence":"100","version":"","icon":"Google Analytics.svg","website":"http://google.com/analytics","categories":["Analytics"]},{"name":"HeadJS","confidence":"50","version":"","icon":"HeadJS.png","website":"http://headjs.com","categories":["JavaScript Frameworks"]},{"name":"Nginx","confidence":"100","version":"1.12.0","icon":"Nginx.svg","website":"http://nginx.org/en","categories":["Web Servers"]},{"name":"PHP","confidence":"100","version":"5.3.29","icon":"PHP.svg","website":"http://php.net","categories":["Programming Languages"]},{"name":"SWFObject","confidence":"100","version":"","icon":"SWFObject.png","website":"http://github.com/swfobject/swfobject","categories":["Miscellaneous"]},{"name":"StatCounter","confidence":"100","version":"","icon":"StatCounter.png","website":"http://www.statcounter.com","categories":["Analytics"]},{"name":"SumoMe","confidence":"100","version":"","icon":"SumoMe.png","website":"http://sumome.com","categories":["Widgets","Marketing Automation"]},{"name":"osCommerce","confidence":"100","version":"","icon":"osCommerce.png","website":"http://www.oscommerce.com","categories":["Ecommerce"]},{"name":"MySQL","confidence":"100","version":"","icon":"MySQL.svg","website":"http://mysql.com","categories":["Databases"]}]}]);

我上传到 MongoDB 中称为网站的集合(它本质上只是 URL 和在其上运行的应用程序的列表)

我使用以下查询返回安装了特定应用程序的所有网站:

db.websites.find({"applications.name":"osCommerce"}).pretty();  

这可以工作并返回使用“osCommerce”的网站列表,但是,当尝试使用 $and 搜索使用多个应用程序的网站时,我的查询不起作用。

我正在尝试使用:

db.inventory.find( { $and: [ {applications.name:{"Apache"}}, 
{applications.name: {"osCommerce"}} ] } ).pretty();

如果运气不好,正确的方法是什么?

最佳答案

你想要$all

db.inventory.find({
"applications.name": { "$all": [ "Apache", "osCommerce" ] }
})

或与 $elemMatch 结合使用如果您想要更多条件,而不仅仅是数组中的 "name":

db.inventory.find({
"applications": {
"$all": ]
{ "$elemMatch": { "name": "Apache" } },
{ "$elemMatch": { "name": "osCommerce" } }
]
}
})

请注意,您在问题中提供的数据没有符合此条件的文档。

如果您想要“任一”值,则为 $in查询:

db.inventory.find({ 
"applications.name": { "$in": [ "Apache", "osCommerce" ] }
})

或者实际上用 $or 表示如果您想要“多个属性”,就像上面一样:

db.inventory.find({
"$or": [
{ "applications": { "$elemMatch": { "name": "Apache" } } }
{ "applications": { "$elemMatch": { "name": "osCommerce" } } }
]
})

关于mongodb - 匹配数组中的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45166500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com