简体   繁体   English

如何在MongoDB中指定_id和对象ID数组作为复合主键?

[英]How to specify _id and array of Object Ids as a composite primary key in MongoDB?

In the MongoDB database, a Collection was created, and inserted with some data: 在MongoDB数据库中,创建了一个Collection,并插入了一些数据:

>>> db.createCollection("Restaurants")
>> { "ok" : 1 }

>> db.Restaurants.insert({ "_id": "coolDinings",
>> "AppliedParameters": [ ObjectId("5617a7a52da21a733547b8da"),
>> ObjectId("5617a5102da21a733547b8d9") ], "Review": "excellent    SeaFood", "Region": "USA" })

MongoDB default behaviour is to consider "_id" as the primary key. MongoDB的默认行为是将“ _id”视为主键。

However, I wanted a composite primary key consisting of the following fields: 但是,我想要一个由以下字段组成的复合主键:

  • -1) "_id" -1)“ _id”
  • -2) "AppliedParameters" -2)“ AppliedParameters”

"AppliedParameters" is an array of ObjectIds that can really vary in terms of number of ObjectIds it stores which can range from 0 to around 20 or 30 ObjectIds. “ AppliedParameters”是一个ObjectId数组,其存储的ObjectId数量实际上可以变化,范围可以从0到大约20或30个ObjectId。

I know the following code will create an index over both the aforementioned fields: 我知道以下代码将在上述两个字段上创建索引:

db.Restaurants.ensureIndex( { "_id": 1,"AppliedParameters": 1 }, { unique:true, sparse:true } ); 

However, my desire was to ensure that the aforementioned line of code will create a composite primary key. 但是,我希望确保上述代码行将创建一个复合主键。

In any case I ran the code. 无论如何,我都运行了代码。

In order to check if the composite key was created, I just reran the very first data insert again because I was hope it would complain by throwing an error related to composite primary key duplicates: 为了检查是否创建了复合键,我只是再次重新运行了第一个数据插入,因为我希望它会抛出与复合主键重复项有关的错误而抱怨:

    >> db.Restaurants.insert({ "_id": "coolDinings",
    >> "AppliedParameters": [ ObjectId("5617a7a52da21a733547b8da"),
    >> ObjectId("5617a5102da21a733547b8d9") ], "Review": "excellent        SeaFood", "Region": "USA" })

However, it only gave the following error: 但是,它仅给出以下错误:

E11000 duplicate key error index: test.Restaurants.$ id dup key: { : "coolDinings" } E11000重复键错误索引:test.Restaurants。$ id dup键:{:“ coolDinings”}

The aforementioned error seems to indicate that the "_id" is duplicated. 前面提到的错误似乎表明“ _id”已重复。 I want the Mongo client to state something along the lines of duplicate composite primary keys are Not allowed. 我希望Mongo客户端声明不允许重复的复合主键。

How do I go about the said task? 我该如何执行上述任务?

There's no "composite" primary keys in mongodb (in a sense that they span multiple fields). mongodb中没有“复合”主键(从某种意义上说,它们跨越多个字段)。 _id is the one and only primary key. _id是唯一的主键。

If you want a complex primary key, composed of several pieces of data, insert them all into the _id field. 如果要由多个数据组成的复杂主键,请将它们全部插入_id字段中。

Note that _id value can't be an array (must be one of primitives or an object). 请注意, _id值不能是数组(必须是基元或对象之一)。

Can't do: 不能做:

{ _id: [1, 2, 3] }

Can do: 可以做:

{ _id: { a: 1, b: [2, 3] } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按主键 ID 的 JSONB 数组指定的顺序选择行? - How to SELECT rows in an order dictated by a JSONB array of primary key IDs? 将多维php数组插入mysql表时,如何添加id(主键)? - How to add id (primary key) when insert a multi-dimensional php array into a mysql table? MongoDB 带有 ID 数组的查询 - MongoDB query with array of IDs 如何根据id创建对象并将内键合并成数组JavaScript - How to create the object based on id and merge the inner key into an array JavaScript 从 MongoDB 给定 ID 的数组中删除 Object - Remove Object from Array in MongoDB given ID 按ID匹配2个数组(用户[用户ID作为键]匹配订单[订单ID数组的值]) - Matching 2 arrays by ids (user[ user id as a key] to orders[value of orders ids array ]) 如何从 mongoDB 中的 _ids 数组加入文档数组 - How to join array of documents from array of _ids in mongoDB 检查一个数组是否包含一个 id 与 id 列表匹配的对象 - Check, whether an array contains an object with id that matches list of ids MongoDB 将组对象转换为 key:value (_id:count) 对象 - MongoDB convert group object to key:value (_id:count) object 如何使用 ruby 中的键将 ID 数组转换为 hash 数组? - How to convert an array of ids to array of hash with a key in ruby?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM