我编写了一个 C++ 函数来将新记录插入到 Mongodb 数据库中的文档中。但是,我无法完成它。我是这个 API 的新手,正在寻求帮助。
我在文档中的记录看起来像这样。现在,我已经编写了一个 mongo shell 命令来执行此操作,但我想使用 C++ API 来实现相同的目的。
{ _id: ObjectId('5541578bcec7d8fd45839197'), id: "sensor1", name: "/temp/s/1", ap: { name: "/ap/1/access/1" } }
C++ API。
void
LocationDb::insert(string sensorId, string sensorName, string routerName) {
BSONObj sensorObj = BSONObjBuilder().genOID().append("id", sensorId) \
.append("name",sensorName).append("ap", "name", routerName).obj();
// This last append is not clear to me.
m_conn.insert("location_db.ldb", sensorObj);
}
有人可以帮助使用正确的 API 来实现上述结果吗?
最佳答案
BSONObj ap = BSONObjBuilder().append("name", routerName).obj();
BSONObj sensorObj = BSONObjBuilder().genOID().append("id", sensorId) \
.append("name",sensorName).append("ap", ap).obj();
关于c++ - 使用 Mongodb C++ API 将记录插入文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29956350/