如果我需要对几个集合执行两个或三个不同的操作,是否有比将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) 假设我有以下类型和变量声明:
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)