小编tic*_*tic的帖子

进行多个 MongoDB 操作的正确方法

如果我需要对几个集合执行两个或三个不同的操作,是否有比将find/update操作链接在一起更好的方法?例如:

db.collection('contactinfos').findOneAndUpdate(
  { _id: ObjectID(contactID) },
  { $set: { sharedWith } }
).then(response => {
  db.collection('users').update(
    { _id: { $in: sharedWith.map(id => ObjectID(id)) } },
    { $addToSet: { hasAccessTo: contactID } },
    { multi: true }
  ).then(response => {
    db.collection('users').update(
      { _id: { $in: notSharedWith.map(id => ObjectID(id)) } },
      { $pull: { hasAccessTo: contactID } },
      { multi: true }
    ).then(response => {
      return res.send({ success: true });
    }).catch(err => {
      logger.error(`in updating sharing permissions for ${contactID} by …
Run Code Online (Sandbox Code Playgroud)

javascript mongodb node.js express

3
推荐指数
1
解决办法
1823
查看次数

TypeScript:从可区分的联合创建新对象而不进行强制转换

链接到以下Q的TS Playground

假设我有以下类型和变量声明:

interface TypeA {
    id: string;
    type: 'A',
    fields: {
        thing1: string;
        thing2: number;
    }
}

const defaultA = {
    thing1: 'hey',
    thing2: 123
}

interface TypeB {
    id: string;
    type: 'B',
    fields: {
        thing3: boolean;
        thing4: number;
    }
}

const defaultB = {
    thing3: true,
    thing4: 456
}

const defaultFields = {
    A: defaultA,
    B: defaultB,
}

type AnyType = TypeA | TypeB
Run Code Online (Sandbox Code Playgroud)

尝试创建一个新的AnyType通过:

const createNewThing = (type: TypeA['type'] | TypeB['type']): AnyType => {
    return …
Run Code Online (Sandbox Code Playgroud)

casting object unions typescript

1
推荐指数
1
解决办法
493
查看次数

标签 统计

casting ×1

express ×1

javascript ×1

mongodb ×1

node.js ×1

object ×1

typescript ×1

unions ×1