[SERVER-23961] $push operation fails on field set to null Created: 28/Apr/16  Updated: 05/May/16  Resolved: 05/May/16

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

Type: Bug Priority: Major - P3
Reporter: nikos efthias [X] Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

db.getCollection('users').update(
  {"token.token":"21jpjg7pqdtigsdektmndqkobthdcktg"},
  {$push:{accounts:"test"}}
)

i run the command above in order to create an array field and the first element inside will be test but i get the error

The field 'accounts' must be an array but is of type NULL in document

i am really confused if i have to create a schema beforehand whats the point of using mongodb over some sql database
i am using in Memory engine

Participants:

 Description   

$push operator wont work

Original Summary

cannot run push if theres no array field



 Comments   
Comment by Kelsey Schubert [ 05/May/16 ]

Hi nikosEfthias,

Thank you for the clarification. The field has been set to a value that is not an array, and consequently any push operations on the field will fail. This is intended behavior and is documented here.

I would recommend modifying your application to refrain from setting this field to null. Depending on your use case, inserting an empty array or leaving field absent if no accounts exist in your application may better serve your needs.

Kind regards,
Thomas

Comment by nikos efthias [X] [ 28/Apr/16 ]

ah now its more clear this is session db on ram and accounts doesnt exist on some accounts so it returns null but still can't mongo handle this kinda stuff why it wont update the null field automatically

Comment by Kelsey Schubert [ 28/Apr/16 ]

Hi nikosEfthias,

Would you please share the document you are updating?
Has the field explicitly been set to null?

I have run a couple of tests on MongoDB 3.2.5, please see my examples below.

If there is no array field the push operation succeeds:

> db.foo.insert({x:1})
WriteResult({ "nInserted" : 1 })
> db.foo.update({x:1},{$push:{y:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.foo.find()
{ "_id" : ObjectId("572193766c6834df7540a3c4"), "x" : 1, "y" : [ 1 ] }

If the field has been explicitly set to null the operation fails:

> db.foo.insert({x:2,y:null})
WriteResult({ "nInserted" : 1 })
> db.foo.update({x:2},{$push:{y:1}})
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 0,
	"nModified" : 0,
	"writeError" : {
		"code" : 16837,
		"errmsg" : "The field 'y' must be an array but is of type NULL in document {_id: ObjectId('572193c96c6834df7540a3c5')}"
	}

Does behavior demonstrated in these examples match what you are seeing?

Thank you,
Thomas

Generated at Thu Feb 08 04:04:58 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.