[SERVER-34245] Save shell command allows multiple documents to be inserted Created: 30/Mar/18  Updated: 29/Oct/23  Resolved: 21/Nov/22

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: 6.3.0-rc0

Type: Bug Priority: Minor - P4
Reporter: Naynish Chaughule Assignee: Sebastien Mendez
Resolution: Fixed Votes: 0
Labels: neweng, query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Studio 3T for MongoDB


Backwards Compatibility: Fully Compatible
Sprint: QE 2022-11-14, QE 2022-11-28
Participants:

 Description   

According to this article https://docs.mongodb.com/manual/reference/method/db.collection.save/

save does not support an array of documents but I could insert multiple documents as follows

db.test.save(
	[
		{
			"_id": 1,
			"fruit": "apple"  	
		},
		{
			"_id": 2,
			"fruit": "banana"  	
		}
	]
)

Now if I run the below command to add '123' at the end of the fruit names, then the save throws duplicate key error. In fact this should have been an upsert since the '_id's are the same.

db.test.save(
	[
		{
			"_id": 1,
			"fruit": "apple 123"  	
		},
		{
			"_id": 2,
			"fruit": "banana 123"  	
		}
	]
)

If I save just a single document then the upsert works as expected

db.test.save(
	{
		"_id": 1,
		"fruit": "apple 123"  	
	}
)

Why is it allowed to upsert multiple documents?



 Comments   
Comment by Githook User [ 21/Nov/22 ]

Author:

{'name': 'Sebastien Mendez', 'email': 'sebastien.mendez@mongodb.com', 'username': 'rakiz'}

Message: SERVER-34245 Throw error when passed array type to save shell command
Branch: master
https://github.com/mongodb/mongo/commit/d54a9d61782679effb17924cbdc2806ffa2166ef

Comment by Asya Kamsky [ 03/Apr/18 ]

Shell save method should give an error when passed "array" type, which it currently does not.

function (obj, opts) {
    if (obj == null)
        throw Error("can't save a null");
 
    if (typeof(obj) == "number" || typeof(obj) == "string")
        throw Error("can't save a number or string");
 
    if (typeof(obj._id) == "undefined") {
        obj._id = new ObjectId();
        return this.insert(obj, opts);
    } else {
        return this.update({_id: obj._id}, obj, Object.merge({upsert: true}, opts));
    }
}

There should probably be an additional check that if it's an object it's not an array.

Leaving in "Needs Triage" since I'm not sure if the query or platform team should own this.

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