且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用rmongodb在R中运行高级MongoDB查询

更新时间:2023-02-18 12:03:58

c()或list()都可以.取决于组件是否被命名以及它们是否具有相同的类型(用于列表).***的办法是查看生成的BSON,看看您是否得到了想要的东西.为了更好地控制生成的对象,请使用mongo.bson.buffer及其上运行的函数.实际上,这就是子查询失败的原因. 注释"被创建为子对象而不是数组. mongo.bson.from.list()很方便,但它不能为您提供相同的控制,有时会猜测从复杂结构生成的内容是错误的.

Either c() or list() can be ok. Depends on whether the components are named and whether they all have the same type (for list). Best thing to do is look at the generated BSON and see if you are getting what you want. For the best control of the generated object use mongo.bson.buffer and the functions that operate on it. In fact this is why the sub-queries are failing. 'comments' is being created as a subobject rather than an array. mongo.bson.from.list() is handy but it doesn't give you the same control and sometimes it guesses wrong about what to generate from complicated structures.

对另一组数据的查询可以像这样纠正:

The query on the other set of data can be corrected like so though:

buf <- mongo.bson.buffer.create()
mongo.bson.buffer.start.object(buf, "name.first")
mongo.bson.buffer.append(buf, "$in", c("Alex", "Horst"))
mongo.bson.buffer.finish.object(buf)
criteria <- mongo.bson.from.buffer(buf)

请注意,您肯定需要在此处使用缓冲区,因为R会阻塞点号.

Note that you definitely need to use a buffer here since R will choke on the dotted name.

我希望这可以解决您的问题.如果您还有其他问题,请告诉我.

I hope this straightens out your problem. Let me know if you have any further questions.