您好,我的收藏中有这种类型的文档
,其中有空间和点
{
"_id" : ObjectId("5d7e3fd812674c2234ff1983"),
"Emp. No" : "ABC",
"Emp. Name" : "DEEPAK DAHIYA",
"Card No" : "24700",
"Department" : "TEst",
"Att Date" : "06-Jun-2019",
"In Time" : "06-Jun-2019 11:10",
"Out Time" : "06-Jun-2019 16:42",
"Status" : "P ",
"Late By " : "02:10",
"Early By " : "01:18",
"Total Hour" : "05:32",
"OT Hour" : "00:00",
"Location" : "Consolidated",
"id" : "f1758761dd4f5295d9075173e83353af"
}
现在我想使用聚合来聚合我的结果。但我的查询没有给出正确的输出,因为我的键中有空格和点
await Dance.aggregate([
{
$group: {
_id: '$["Emp. No"]',
data: {
$addToSet: { attendance: "$['Att Date']", Status: "$Status", id: "$id" }
}
}
},
{ $project: { '["Emp. No"]': "$_id", _id: 0, data: 1 } }
]);
已经尝试过方括号但不起作用。
更新的查询:
const result = await Dance.aggregate([
{
$group: {
_id: "$Emp. No",
data: {
$addToSet: { attendance: "$Att Date", Status: "$Status", id: "$id" }
}
}
},
{ $project: { "Emp. No": "$_id", _id: 0, data: 1 } }
]);
也无法正常工作
最佳答案
你can have fields with spaces以及fields with underscores - 你不能在字段中包含句点,例如:escaped query和 non-escaped query ..
现在,有一些警告......只要您使用 Mongo 3.6+
并且只要字段包含句点 is not a top level field, and is the only 'nested' field ,它会起作用。如果您尝试使用多个嵌套字段,it will not work ..
您可以read more on the subject here ...
总而言之,您应该谨慎行事并删除句点,但您可以保留允许的空格。
关于javascript - 当key有空格和点时如何在mongodb中查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57945337/