C ++写入mongo,字符串字段在聚合管道中不起作用

时间:2018-12-30 18:16:22

标签: c++ mongodb aggregation-framework unicode-string otl

** 快速摘要:C ++应用程序使用OTL4从SQL Server加载数据,使用mongocxx bulk_write写入Mongo,这些字符串似乎以某种方式被弄乱了,因此它们在聚合管道中不起作用(否则看起来很好)。 **

我有一个简单的Mongo集合,当我投影多个字段时,它似乎在聚合管道中表现不佳。这是一个琐碎的文档,没有嵌套,字段只是双精度和字符串。

前2个查询按预期工作:

> db.TemporaryData.aggregate( [ { $project :  {  ParametersId:1 } } ] )
{ "_id" : ObjectId("5c28f751a531251fd0007c72"), "ParametersId" : 526988617 }
{ "_id" : ObjectId("5c28f751a531251fd0007c73"), "ParametersId" : 526988617 }
{ "_id" : ObjectId("5c28f751a531251fd0007c74"), "ParametersId" : 526988617 }
{ "_id" : ObjectId("5c28f751a531251fd0007c75"), "ParametersId" : 526988617 }
{ "_id" : ObjectId("5c28f751a531251fd0007c76"), "ParametersId" : 526988617 }

> db.TemporaryData.aggregate( [ { $project :  {  Col1:1 } } ] )
{ "_id" : ObjectId("5c28f751a531251fd0007c72"), "Col1" : 575 }
{ "_id" : ObjectId("5c28f751a531251fd0007c73"), "Col1" : 579 }
{ "_id" : ObjectId("5c28f751a531251fd0007c74"), "Col1" : 616 }
{ "_id" : ObjectId("5c28f751a531251fd0007c75"), "Col1" : 617 }
{ "_id" : ObjectId("5c28f751a531251fd0007c76"), "Col1" : 622 }

但是合并后并没有按预期返回两个字段。

> db.TemporaryData.aggregate( [ { $project :  {  ParametersId:1, Col1:1 } } ] )
{ "_id" : ObjectId("5c28f751a531251fd0007c72"), "ParametersId" : 526988617 }
{ "_id" : ObjectId("5c28f751a531251fd0007c73"), "ParametersId" : 526988617 }
{ "_id" : ObjectId("5c28f751a531251fd0007c74"), "ParametersId" : 526988617 }
{ "_id" : ObjectId("5c28f751a531251fd0007c75"), "ParametersId" : 526988617 }
{ "_id" : ObjectId("5c28f751a531251fd0007c76"), "ParametersId" : 526988617 }

它似乎特定于ParametersId字段,例如,如果我选择其他2个字段就可以。

> db.TemporaryData.aggregate( [ { $project :  {  Col1:1, Col2:1 } } ] )
{ "_id" : ObjectId("5c28f751a531251fd0007c72"), "Col1" : 575, "Col2" : "1101-2" }
{ "_id" : ObjectId("5c28f751a531251fd0007c73"), "Col1" : 579, "Col2" : "1103-2" }
{ "_id" : ObjectId("5c28f751a531251fd0007c74"), "Col1" : 616, "Col2" : "1300-3" }
{ "_id" : ObjectId("5c28f751a531251fd0007c75"), "Col1" : 617, "Col2" : "1300-3" }
{ "_id" : ObjectId("5c28f751a531251fd0007c76"), "Col1" : 622, "Col2" : "1400-3" }

由于某些原因,当我包含ParametersId字段时,管道中的所有地狱都松散了:

> db.TemporaryData.aggregate( [ { $project :  {  ParametersId:1, Col2:1, Col1:1, Col3:1 } } ] )
{ "_id" : ObjectId("5c28f751a531251fd0007c72"), "ParametersId" : 526988617, "Col1" : 575 }
{ "_id" : ObjectId("5c28f751a531251fd0007c73"), "ParametersId" : 526988617, "Col1" : 579 }
{ "_id" : ObjectId("5c28f751a531251fd0007c74"), "ParametersId" : 526988617, "Col1" : 616 }
{ "_id" : ObjectId("5c28f751a531251fd0007c75"), "ParametersId" : 526988617, "Col1" : 617 }
{ "_id" : ObjectId("5c28f751a531251fd0007c76"), "ParametersId" : 526988617, "Col1" : 622 }

数据库版本和数据:

> db.version()
4.0.2
> db.TemporaryData.find()
{ "_id" : ObjectId("5c28f751a531251fd0007c72"), "CellId" : 998909269, "ParametersId" : 526988617, "Order" : 1, "Col1" : 575, "Col2" : "1101-2", "Col3" : "CHF" }
{ "_id" : ObjectId("5c28f751a531251fd0007c73"), "CellId" : 998909269, "ParametersId" : 526988617, "Order" : 1, "Col1" : 579, "Col2" : "1103-2", "Col3" : "CHF" }
{ "_id" : ObjectId("5c28f751a531251fd0007c74"), "CellId" : 998909269, "ParametersId" : 526988617, "Order" : 1, "Col1" : 616, "Col2" : "1300-3", "Col3" : "CHF" }
{ "_id" : ObjectId("5c28f751a531251fd0007c75"), "CellId" : 998909269, "ParametersId" : 526988617, "Order" : 36, "Col1" : 617, "Col2" : "1300-3", "Col3" : "CHF" }
{ "_id" : ObjectId("5c28f751a531251fd0007c76"), "CellId" : 998909269, "ParametersId" : 526988617, "Order" : 1, "Col1" : 622, "Col2" : "1400-3", "Col3" : "CHF" }

更新:使用字段名称加引号没有区别。我在mongo.exe命令行中输入了以上所有内容,但是在我的C ++应用程序中看到了相同的行为,但管道稍微复杂一些(投影所有字段以保证顺序)。

同一款应用程序实际上首先是在创建数据-有人知道会出错的信息吗?全部使用mongocxx lib。

**更新**

结果是我处理字符串时出了点问题。在数据中没有字符串字段,一切都很好。因此,尽管它们看起来和行为正确,但我还是以某种方式弄皱了我的字符串,它们在聚合管道中表现不佳。我正在使用mongocxx :: collection.bulk_write编写标准的std :: string,这些字符串是通过OTL4标头从sql server中加载的。当它们在内部存储时,中间会有一个strncpy_s。我似乎无法创建一个简单的可复制示例。

2 个答案:

答案 0 :(得分:0)

为了安全起见,请不要将投影与严格格式化的json结合使用:(在键中添加引号)

db.TemporaryData.aggregate( [ { $project :  {  "ParametersId":1, "Col1":1 } } ] )

答案 1 :(得分:0)

最后发现问题是损坏的文档,这是因为我在插入时使用bulk_write进入了数据库,但是导致了这种奇怪的行为。我改用insert_many,这引发了文档损坏,然后可以查找该错误。

文档已损坏,因为我多次写入相同的字段值数据,这似乎破坏了我用来构造它们的bsoncxx :: builder :: stream :: document。