简体   繁体   English

将项目添加到Typescript中的数组

[英]Adding item to an array in Typescript

I am working in TypeScript with MongoDB and I am trying to add an item to an array. 我正在使用TypeScript和MongoDB进行操作,并且正在尝试将一个项目添加到数组中。

registeredUsers.update({ guid: ObjectId(req.cookies._id) }, {
    $addToSet: {
        favorites: [comicID]
    }
});

This is the code I have currently, and I am trying to add the comicID to an array called favorites that is in registeredUsers . 这是我当前拥有的代码,我正在尝试将comicID添加到一个名为registeredUsers favorites的数组中。 At the moment it does not seem to be adding to the array at all, so when I try to look at the array it is empty. 目前,它似乎根本没有添加到数组中,因此当我尝试查看数组时,它为空。

Your code seems fine. 您的代码似乎很好。 I suspect the reason you are not getting updates is because your search condition is not matching any documents. 我怀疑您没有得到更新的原因是因为您的搜索条件与任何文档都不匹配。

Have you tried the following statement: 您是否尝试过以下语句:

 db.registeredUsers.find({ guid: ObjectId(req.cookies._id) })

If your search condition is valid, you should get the "desired" document back. 如果您的搜索条件有效,则应该取回“所需”文档。

Here are my examples: 这是我的示例:

First create a doc. 首先创建一个文档。

> db.registeredUsers.insert({ "mycustom_id":1, "favorites":[1,2,3]})
WriteResult({ "nInserted" : 1 })

Check to see if the doc looks right. 检查文档是否正确。

> db.registeredUsers.find()
{ "_id" : ObjectId("56f88aa1d14e4832a027f625"), "mycustom_id" : 1, "favorites" : [ 1, 2, 3 ] }

Do an update based on one of the columns that I defined. 根据我定义的列之一进行更新。

> db.registeredUsers.update({"mycustom_id" : 1}, { $addToSet: { favorites: 5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Check the update results: 检查更新结果:

> db.registeredUsers.find()
{ "_id" : ObjectId("56f88aa1d14e4832a027f625"), "mycustom_id" : 1, "favorites" : [ 1, 2, 3, 5 ] }

Now, do an update based on the "_id" field. 现在,基于“ _id”字段进行更新。

> db.registeredUsers.update({"_id":  ObjectId("56f88aa1d14e4832a027f625") }, { $addToSet: { favorites: 6}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Checking the result: 检查结果:

> db.registeredUsers.find()
{ "_id" : ObjectId("56f88aa1d14e4832a027f625"), "mycustom_id" : 1, "favorites" : [ 1, 2, 3, 5, 6 ] }

Another example where the update is expected to create the array: 预期更新将创建数组的另一个示例:

> db.registeredUsers.insert({"a":  1})
WriteResult({ "nInserted" : 1 })
> db.registeredUsers.find()
{ "_id" : ObjectId("56f88aa1d14e4832a027f625"), "mycustom_id" : 1, "favorites" : [ 1, 2, 3, 5, 6 ] }
{ "_id" : ObjectId("56f88be6d14e4832a027f626"), "a" : 1 }

Updating based on "_id" again in a situation where the array favorites does not exist prior to the update 在更新之前不存在阵列收藏夹的情况下,再次基于“ _id”进行更新

> db.registeredUsers.update({"_id" : ObjectId("56f88be6d14e4832a027f626")}, { $addToSet: { favorites: 6}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Check the results: 检查结果:

> db.registeredUsers.find()
{ "_id" : ObjectId("56f88aa1d14e4832a027f625"), "mycustom_id" : 1, "favorites" : [ 1, 2, 3, 5, 6 ] }
{ "_id" : ObjectId("56f88be6d14e4832a027f626"), "a" : 1, "favorites" : [ 6 ] }

As you can see my queries are identical to yours. 如您所见,我的查询与您的查询相同。 You are comparing "guid" and I am comparing "_id". 您正在比较“ guid”,而我正在比较“ _id”。 I am suspicious that "guid" does not contain what you expect it to contain, which leads to your "search condition" to the update to return 0 documents, consequently 我怀疑“ guid”不包含您期望的内容,这导致您对更新的“搜索条件”返回0个文档,因此

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM