I have case of collection that holds "item" data, with required node "owner" and optional node "status", e.g.
{ _id: 123, item: {some: "data 123" }, owner: {id: 456} }
{ _id: 124, item: {some: "data 124" }, owner: {id: 789}, status: { ok: 1} }
and when insert a new record, if the new owner.id
equals the old owner.id
, then i want the new record to have the same status
expressed in SQL it'd be something like
INSERT INTO mytable
(
id,
item_some,
owner_id,
status_ok
)
VALUES (
125,
'data 125',
789,
(SELECT COALESCE(status_ok, null) FROM mytable WHERE owner_id=789 LIMIT 1)
);
I'm looking at $expr
, but not sure how to use it in db.collection.insert()
...
Thanks in advance