[SERVER-46422] $addField doesn't allow an empty object as a value Created: 26/Feb/20  Updated: 27/Oct/23  Resolved: 03/Mar/20

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 4.2.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Ha Ha Assignee: Ian Boros
Resolution: Works as Designed Votes: 0
Labels: qexec-team
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

db.coll2.updateOne({"_id": ObjectId("5e568d732c8a252f458aaae1")}, [{$set: {"_id": ObjectId("5e568d732c8a252f458aaae1"), some_message: "message", attributes: {}, last_update_at: "$$NOW"}}], {upsert: true})

Sprint: Query 2020-03-23
Participants:
Case:

 Description   

> db.coll2.updateOne({"_id": ObjectId("5e568d732c8a252f458aaae1")}, [{$set: {"_id": ObjectId("5e568d732c8a252f458aaae1"), some_message: "message", attributes: {}, last_update_at: "$$NOW"}}], {upsert: true})
2020-02-26T19:24:55.487+0300 E  QUERY    [js] WriteError({
	"index" : 0,
	"code" : 40180,
	"errmsg" : "Invalid $addFields :: caused by :: an empty object is not a valid value. Found empty object at path attributes",
	"op" : {
		"q" : {
			"_id" : ObjectId("5e568d732c8a252f458aaae1")
		},
		"u" : [
			{
				"$set" : {
					"_id" : ObjectId("5e568d732c8a252f458aaae1"),
					"some_message" : "message",
					"attributes" : {
 
					},
					"last_update_at" : "$$NOW"
				}
			}
		],
		"multi" : false,
		"upsert" : true
	}
}) :
WriteError({
	"index" : 0,
	"code" : 40180,
	"errmsg" : "Invalid $addFields :: caused by :: an empty object is not a valid value. Found empty object at path attributes",
	"op" : {
		"q" : {
			"_id" : ObjectId("5e568d732c8a252f458aaae1")
		},
		"u" : [
			{
				"$set" : {
					"_id" : ObjectId("5e568d732c8a252f458aaae1"),
					"some_message" : "message",
					"attributes" : {
 
					},
					"last_update_at" : "$$NOW"
				}
			}
		],
		"multi" : false,
		"upsert" : true
	}
})
WriteError@src/mongo/shell/bulk_api.js:458:48
mergeBatchResults@src/mongo/shell/bulk_api.js:855:49
executeBatch@src/mongo/shell/bulk_api.js:919:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.updateOne@src/mongo/shell/crud_api.js:600:17
@(shell):1:1

It raises Invalid $addFields :: caused by :: an empty object is not a valid value. Found empty object at path attributes with 40180 error code which is weird because simple insert works fine with the same document.

Why can't I insert empty field(with {} value) into document? Is there another way to insert a new document to collection with $$NOW(as I know it only usable in update operations) value?



 Comments   
Comment by Asya Kamsky [ 16/Jun/20 ]

I believe $literal (which we document) would be preferable to $const (internal term)

Comment by Ian Boros [ 03/Mar/20 ]

Filed DOCS-13489 to maybe update/improve the docs. I will close this as WaD.

Comment by Ian Boros [ 03/Mar/20 ]

ha4er122@gmail.com

This works as designed. What you probably want to do to avoid this error is to use $const:

db.c.aggregate([{$addFields: {attributes: {$const: {}}}}]) 

 

My guess for the motivation behind this behavior is that we already use objects to for representing expressions (like {a: {$add: [1, 2]}}). In projections they're also used to represent projections of sub-objects. Having an empty object indicate the special case of "literal empty object" would just make the language a bit harder to reason about.

 

Let me know if you have other questions about this!

Comment by Carl Champain (Inactive) [ 28/Feb/20 ]

Hi ha4er122@gmail.com,

Thank you for the report.
We're assigning this ticket to the appropriate team for further investigation. Updates will be posted on this ticket as they happen.

Kind regards,
Carl

Generated at Thu Feb 08 05:11:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.