Hi @David_Lange,
It would be helpful to include the error message and your specific version of MongoDB server for context, but I expect the error you are getting is similar to:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "wrong type for field (db) object != string",
"code" : 13111,
"codeName" : "Location13111"
}
JavaScript objects may have different representation depending on the usage context. For example, in the mongo
shell, calling print()
on an object will implicitly call object.tojson()
(which is a feature of the mongo
shell). You can also invoke standard JavaScript methods like object.toString()
.
I think you want to keep your db_ods
variable as a DB object so you can call collection methods, but should explicitly call toString()
to get the expected representation in the context of your aggregation query:
db_pim.item_collection.aggregate([
{ "$project": {
"PartId": "$windecsPartId"
}},
{ $out: { db: db_ods.toString(), coll: "stg_c1" }}
]);
Note: all of the JavaScript in this example is being evaluated in the mongo
shell before the aggregation query is sent to the MongoDB server. As @Prasad_Saya mentioned, you can check the result type of a statement using the typeof
JavaScript operator:
> typeof(db_ods)
object
> typeof(db_ods.toString())
string
Regards,
Stennie