我有以下文件:
{
_id : 1,
key1 : {
samekeyA : "value1",
samekeyB : "value2"
},
key2 : {
samekeyA : "value3",
samekeyB : "value4"
},
key3 : {
samekeyA : "value5",
samekeyB : "value6"
}
}
Run Code Online (Sandbox Code Playgroud)
以上; key1,key2并被key3用来证明我不知道完整的密钥,除了它的前缀;但内在的钥匙samekeyA和samekeyB我知道的那些。我需要查询,例如:db.coll.find({"key*.samekeyA":"value1"})。
我认为没有一种 mongo 方式 - 正则表达式键查询? - 来实现这一点,所以有什么想法吗?我应该改造我的文档-tree-吗?
鉴于以下文件:
{
"_id" : ObjectId("53cd79bb300ccae6b3904402"),
"name" : "test product",
"sku" : "product-1",
"price" : 35,
"cost" : 12,
"max_cpc" : 100,
"price_in_cents" : 3500,
"cost_in_cents" : 1200,
"max_cpc_in_cents" : 10000,
"clicks" : [
{
"date" : ISODate("2014-04-25T00:00:00Z"),
"clicks" : 2,
"channel" : "google",
"campaign" : "12345687",
"group" : "987654321"
},
{
"date" : ISODate("2014-04-25T00:00:00Z"),
"clicks" : 3,
"channel" : "google",
"campaign" : "8675309",
"group" : "9035768"
},
{
"date" : ISODate("2014-04-24T00:00:00Z"),
"clicks" : 1,
"channel" : "google",
"campaign" : "8675309", …Run Code Online (Sandbox Code Playgroud) 我需要计算一组文件的加权平均值.每个文档在单独的字段中包含权重和值.这是一个例子:我有两个代表销售交易的文件.
{
quantity : 3,
price : 2
}
{
quantity : 9,
price : 6
}
Run Code Online (Sandbox Code Playgroud)
我想找到这两笔交易的平均价格.这是加权平均值,其中权重是数量,价值是价格.这可以通过计算得出
AveragePrice = (3 * 2 + 9 * 6 ) / (3 + 9).
如何使用聚合框架执行此计算?
假设我有以下 MongoDB 文档。
{
(...)
"services": {
"TCP80": {
"data": [{
"status": 1,
"delay": 3.87,
"ts": 1308056460
},{
"status": 1,
"delay": 2.83,
"ts": 1308058080
},{
"status": 1,
"delay": 5.77,
"ts": 1308060720
}]
}
}}
Run Code Online (Sandbox Code Playgroud)
我希望检索“数据”数组中“ts”值大于“1308056460”的所有条目。因此,预期的结果是,
[
{
"status": 1,
"delay": 2.83,
"ts": 1308058080
},{
"status": 1,
"delay": 5.77,
"ts": 1308060720
}
]
Run Code Online (Sandbox Code Playgroud)
一种方法是使用 MongoDB 聚合函数。
db.test.aggregate(
{$match : {}},
{$unwind: "$services.TCP80.data"},
{$match: {"services.TCP80.data.ts": {$gte: 1308060720}}}
);
Run Code Online (Sandbox Code Playgroud)
另一种方法是将整个数据数组检索到中间层(node.js 应用程序)并运行循环以过滤掉我想要的值。
撇开实现的简单性不谈,哪种方法更有效或产生更快的结果?为什么?
我在 mongoDB 中有一个类似于下面的大数据集
我想在 MongoDB 中运行一个聚合,它相当于这个 SQL:
SELECT SUM(cores) from machines
WHERE idc='AMS' AND cluster='1' AND type='Physical';
Run Code Online (Sandbox Code Playgroud)
我如何在 MongoDB 中进行此操作?
[
{
"_id" : "55d5dc40281077b6d8af1bfa",
"hostname" : "x",
"domain" : "domain",
"description" : "VMWare ESXi 5",
"cluster" : "1",
"type" : "Physical",
"os" : "EXSi",
"idc" : "AMS",
"environment" : "DR",
"deviceclass" : "host",
"cores" : "64",
"memory" : "256",
"mounts" : [ ],
"roles" : [
"ESX-HOST"
],
"ipset" : {
"backnet" : "1"
},
"frontnet" : [ ], …Run Code Online (Sandbox Code Playgroud) 我有mongo类型的文件-
{
"_id" : ObjectId("77f02ee61df85c423b6a4e79"),
"client" : "1"
"type" : "type1",
"hierarchy" : "hier1",
"creationDate" : ISODate("2015-09-09T13:06:44Z"),
"model" : "m1"
},
{
"_id" : ObjectId("77f02ee61df85c423b6a4e80"),
"client" : "1"
"type" : "type1",
"hierarchy" : "hier1",
"creationDate" : ISODate("2015-09-10T14:06:44Z"),
"model" : "m2"
},
{
"_id" : ObjectId("77f02ee61df85c423b6a4e81"),
"client" : "1"
"type" : "type1",
"hierarchy" : "hier2",
"creationDate" : ISODate("2015-09-10T13:06:44Z"),
"model" : "m3"
},
{
"_id" : ObjectId("77f02ee61df85c423b6a4e82"),
"client" : "2"
"type" : "type2",
"hierarchy" : "hier2",
"creationDate" : ISODate("2015-09-10T14:06:44Z"),
"model" …Run Code Online (Sandbox Code Playgroud) My main object looks like this:
{
"_id" : ObjectId("56eb0a06560fd7047318465a"),
...
"intervalAbsenceDates" : [
DBRef("ScheduleIntervalContainer", ObjectId("56eb0a06560fd7047318463b")),
DBRef("ScheduleIntervalContainer", ObjectId("56eb0a05560fd70473184467")),
DBRef("ScheduleIntervalContainer", ObjectId("56eb0a05560fd70473184468")),
DBRef("ScheduleIntervalContainer", ObjectId("56eb0a05560fd70473184469")),
Run Code Online (Sandbox Code Playgroud)
An embedded ScheduleIntervalContainer object looks like this:
{
"_id" : ObjectId("56eb0a06560fd7047318463b"),
"end" : ISODate("2022-08-23T07:06:00Z"),
"available" : true,
"confirmation" : true,
"start" : ISODate("2022-08-19T09:33:00Z")
}
Run Code Online (Sandbox Code Playgroud)
Now I will query all ScheduleIntervalContainers where start and end is in a range. I have tried a lot but I not even can query one ScheduleIntervalContainer by id. This is my approach:
db.InstitutionUserConnection.find( …Run Code Online (Sandbox Code Playgroud) 在我的查询中,concat关键字不起作用,它返回null。
这是查询:-
db.leads.aggregate([
{$project:{
_id:0,
status:1,
stage:1,
"todo.title":1,
created:{
day:{$substr:["$createdOn",8,2]},
month:{$substr:["$createdOn",5,2]},
year:{$substr:["$createdOn",0,4]}
},
myDate:{$concat:["$created.day","-","$created.month","-","$created.day"]}
//----above $concat is not working-----//
//--i want that `myDate` should be "12-09-2016"----//
}
}
])
Run Code Online (Sandbox Code Playgroud)
这是查询输出:
{
"stage" : "Prospect",
"todo" : [],
"status" : "OPEN",
"created" : {
"day" : "12",
"month" : "09",
"year" : "2016"
},
"myDate" : null
//--here i want that `myDate` should be "12-09-2016"----//
}
Run Code Online (Sandbox Code Playgroud)
在mongodb 中将createdOn字段数据存储为Date类型,即
mongodb mongodb-query aggregation-framework mongodb-aggregation
我正在做一个查询,从我的书集中提取属性“ titlu”的不同首字母,以便根据书籍的“ titlu”属性的首字母进行分组。我有一些以UTF-8字符开头的标题,例如Î,?,?等,但出现此错误:
显而易见的问题是:如何消除该错误?有两个可接受的选项:
但是,转换必须在mongo查询中完成,因为我还需要计数标题。(对于解决方案2,我们需要字母I,例如,将以I开头的标题的出现与以Î开头的标题的出现进行求和)。
我是Mongo的新手!请帮助我如何在Mongo中退出加入
Sql语句:
Select * from TableA left Join TableB
on (TableA.col1 = TableB.col1 AND TableB.col2 = "ABC")
Run Code Online (Sandbox Code Playgroud)
请向我提供等效的Mongo查询!!!
提前致谢 !