InsertMany插入并忽略文档架构中的“唯一”要求

时间:2019-10-29 09:44:50

标签: node.js mongodb mongoose mongoose-schema

考虑架构负责人:

bool doIntersect(Point p1, Point q1, Point p2, Point q2)   
{ 
// Find the four orientations needed for general and 
// special cases 
int o1 = orientation(p1, q1, p2); 
int o2 = orientation(p1, q1, q2); 
int o3 = orientation(p2, q2, p1); 
int o4 = orientation(p2, q2, q1); 

// General case 
if (o1 != o2 && o3 != o4) 
    return true; 

// Special Cases 
// p1, q1 and p2 are colinear and p2 lies on segment p1q1 
if (o1 == 0 && onSegment(p1, p2, q1)) return true; 

// p1, q1 and q2 are colinear and q2 lies on segment p1q1 
if (o2 == 0 && onSegment(p1, q2, q1)) return true; 

// p2, q2 and p1 are colinear and p1 lies on segment p2q2 
if (o3 == 0 && onSegment(p2, p1, q2)) return true; 

// p2, q2 and q1 are colinear and q1 lies on segment p2q2 
if (o4 == 0 && onSegment(p2, q1, q2)) return true; 

return false; // Doesn't fall in any of the above cases 
} 

以及插入Mongo的动作:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const LeadsSchema = new Schema({
  supplier: {
    type: Schema.Types.ObjectId,
    ref: "suppliers",
    required: false
  },
  PhoneNumber: {
    type: String,
    required: true,
    unique: true
  },
  BusinessName: {
    type: String,
    required: true
  } ...
  ...
  ...
});

   module.exports = Lead = mongoose.model(
  "leads",
  LeadsSchema.index({ PhoneNumberMasque: 1 }, { expireAfterSeconds: 3600 })
);

问题在于,即使我将 Leads.insertMany(leadsToInsert, { ordered: true }) .then(function(docs) { console.log("Number of documents inserted: " + docs.length); return res .status(200) .json({ msg: "Documents uploaded to MongoDB Successfully!" }); }) .catch(function(err) { // error handling here console.log("Failed to insert Bulk..." + err.message); return res.status(500).json({ msg: err.message }); }); 设置为索引并且是唯一的,Mongo仍会插入所有文档,无论是否重复。

为什么?

0 个答案:

没有答案