I think you can follow my steps to check if it has created correct unique index for your cosmos mongoapi. Pls note, unique index can be created when the collection has no document. So if you try to add unique index, you may delete and create a new collection first.
First, you need to check if you've created unique index correctly. Go to your database in azure portal, open console panel in your browser(press F12), then open the setting view, see my screenshot below:

If the unique index exists, I think it will be ok for your program to prevent insert duplicate item. At least in my side it worked well. I'll show my code here, you may refer to it.

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var url = 'copy from quick start-> nodejs-> PRIMARY CONNECTION STRING';
var createUniqueIndex = function(db){
db.collection('families').createIndex({"lastName": 1} ,{ unique: true });
console.log("============create index succssfully============");
}
var insertDocument = function(db, callback) {
db.collection('families').insertOne( {
"id": "AndersenFamily2",
"lastName": "Andersen",
"parents": [
{ "firstName": "Thomas" },
{ "firstName": "Mary Kay" }
],
"children": [
{ "firstName": "John", "gender": "male", "grade": 7 }
],
"pets": [
{ "givenName": "Fluffy" }
],
"address": { "country": "USA", "state": "WA", "city": "Seattle" }
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the families collection.");
callback();
});
};
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
var db = client.db('familiesdb');
// createUniqueIndex(db);
insertDocument(db, function() {
client.close();
});
});