Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion A unique index on an array

Date: Fri, 20 Jul 2012 12:10:37 -0700 (PDT)
From: dbrand666 <D...@rewind.me>
To: mongoose-orm@googlegroups.com
Message-Id: <ad9c07d1-2563-4ca4-8cbe-5dfcf8a0acf1@googlegroups.com>
Subject: A unique index on an array
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_33_26257412.1342811437345"

------=_Part_33_26257412.1342811437345
Content-Type: multipart/alternative; 
	boundary="----=_Part_34_436388.1342811437345"

------=_Part_34_436388.1342811437345
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

My schema contains an array or strings.  I want to put a unique, sparse 
index on this array, like so:

new Schema({
  array_of_strings: {type: [String], unique: true, sparse: true}
});

The problem I'm running into is that if array_of_strings is empty, Mongoose 
includes the empty array in the document. This leads to duplicate key 
errors. It there a way to tell Mongoose not to include empty objects when 
writing documents?

------=_Part_34_436388.1342811437345
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

My schema contains an array or strings.&nbsp; I want to put a unique, sparse index on this array, like so:<br><br>new Schema({<br>&nbsp; array_of_strings: {type: [String], unique: true, sparse: true}<br>});<br><br>The problem I'm running into is that if array_of_strings is empty, Mongoose includes the empty array in the document. This leads to duplicate key errors. It there a way to tell Mongoose not to include empty objects when writing documents?<br>
------=_Part_34_436388.1342811437345--

------=_Part_33_26257412.1342811437345--