Move doc1.field to doc2.field on same id. aggregate error in mongo4.4: Cannot $merge to internal database: local

l have 2 doc.
doc1:[ {a:1,b:10} {a:2:b:20}]
doc2:[ {a:1,c:100} {a:2:c:200}]

I want move doc2.c to doc1 when doc.a is same.

this is my command:
db.doc2.aggregate([ { $merge: { into: “doc1”, on: “a”, let: { c: “$c” }, whenMatched: [ { $addFields: { c: “$$c” } }], whenNotMatched: “discard” } }])

in mongo4.2 it works
in mongo4.4 is error: Cannot $merge to internal database: local.

is my command wrong?

There are terminology issues with your post that makes it hard to understand.

You write that you have 2 docs, but use $merge which is a collection operation.

You also write that it works in 4.2.

Assuming doc1 and doc2 are really collections, I suspect that you forgot to switch to the database that contains your collections with the use command and that you are using the local database which is reserved. It worked in 4.2 simply because you did the appropriate use TheCorrectDatabase before the $merge.

sorry for my bad english…
i means :
doc1 and doc2 are collections. i want to copy doc2.d to doc1.d where doc1.a == doc2.a
this is my pic.

in mongo4.4:

in mongo4.2:

it is nice thing you use mongosh because it shows you which database is currently active: local is your current one because at some point you have used use local command.

as @steevej stated above, it is reserved for internal use, and you are supposed to NOT use it. check this link for more details The local Database — MongoDB Manual

Please use/create some other database name for your needs, such as mergingdb and switch to it with use mergingdb for your operations. rest assured your query works that way.

1 Like

oh oh, i got it.
thank you all for help me !!!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.