0

I have written a C++ function to insert a new record into the document in a Mongodb database. However, I could not finish it. I am new to this API and seeking help.

My record into docuement would look like this. For now, I have written a mongo shell command to do this, but I want to achieve same using 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);
}

Can someone please help with the correct API to achieve the above result?

1 Answer 1

1
BSONObj ap = BSONObjBuilder().append("name", routerName).obj();
BSONObj sensorObj = BSONObjBuilder().genOID().append("id", sensorId) \
        .append("name",sensorName).append("ap", ap).obj();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.