Skip to content

Commit ecd1143

Browse files
committed
CXX-180 Generate _id automatically for InsertWriteOperations
1 parent 779a788 commit ecd1143

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/mongo/client/insert_write_operation.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace mongo {
2727
} // namespace
2828

2929
InsertWriteOperation::InsertWriteOperation(const BSONObj& doc)
30-
: _doc(doc)
30+
: _doc(_ensureId(doc))
3131
{}
3232

3333
WriteOpType InsertWriteOperation::operationType() const {
@@ -63,4 +63,14 @@ namespace mongo {
6363
obj->appendElements(_doc);
6464
}
6565

66+
BSONObj InsertWriteOperation::_ensureId(const BSONObj& doc) {
67+
if (doc.hasField("_id"))
68+
return doc;
69+
70+
BSONObjBuilder bob;
71+
bob.append("_id", OID::gen());
72+
bob.appendElements(doc);
73+
return bob.obj();
74+
}
75+
6676
} // namespace mongo

src/mongo/client/insert_write_operation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ namespace mongo {
3636
virtual void appendSelfToBSONObj(BSONObjBuilder* obj) const;
3737

3838
private:
39+
static BSONObj _ensureId(const BSONObj& doc);
40+
3941
const BSONObj _doc;
4042
};
4143

0 commit comments

Comments
 (0)