-2

I have searched for how to create a unique index, but I haven't seen any guides doing it. I want to put a unique key on three variables in my document. x, y, and z. I'm storing coordinates, and need to make sure I can't have duplicate x, y, and z coords. Anything helps.

Edit: I'm trying to this SQL in MongoDB:

CREATE TABLE mapPosition(
  x INT NOT NULL,
  y INT NOT NULL,
  z INT NOT NULL
  CONSTRAINT mapPosition_x_y_z_pk PRIMARY KEY (x, y, z)
);
Jeff smith
  • 147
  • 7

1 Answers1

0

Using HashMap is nice way to get this functionality into your program. Its a data structure that allows for key value pairs and each key needs to be unique.

luckydog32
  • 909
  • 6
  • 13
  • that wouldn't prevent the map from having duplicate `x`, `y` and `z` coords. Only the `key` needs to be unique (and if you try to put the coords in an object, `HashSet` and `HashMap` are not compatible with `equals()`...) – Paul Lemarchand Oct 13 '17 at 04:34
  • I really am just looking for the code to do it in Java. Possibly via createIndex but I'm not sure. – Jeff smith Oct 13 '17 at 12:59