我有一个查询来查找用户CreatedBy
是否在SharedWith
中。我想反查询,以检查CreatedBy
中是否没有SharedWith
。
[
{
"$match": {
"$and": [
{
"$and": [
{
"SharedWith": {
"$exists": true
}
},
{
"$expr": {
"$in": [
"$CreatedBy",
"$Multi_User"
]
}
}
]
}
]
}
}
]
MongoDB不支持直接$nin
或$not
进行$and
查询。
任何想法如何实现这一目标。
用户文档如下所示,
Collection = [
{"CreatedBy":
{"_id": "User001",
"Email": "user@eg.com",
"Name": "User001"},
"SharedWith": [
{"_id": "User001",
"Email": "user@eg.com",
"Name": "User001"},
{"_id": "User002",
"Email": "user@eg.com",
"Name": "User002"},
{"_id": "User003",
"Email": "user@eg.com",
"Name": "User003"},
]}
]
答案 0 :(得分:2)
$nin
是查询运算符,不是聚合运算符,并且$expr
仅支持const array = [{
district: "north"
store: "1001,1002"
},
{
district: "south"
store: "1003"
},{
district: "west"
store: "1004"
}
]
function apiCall(array) {
array.forEach(element => {
let stores = element.store.toString()
let query = `SELECT store, sku, tot_sales, price
FROM big-query-table
WHERE
store IN (${stores})`
asyncQuery(query)
.then(resp => {
console.log(resp)
}).catch(err => {
console.error('ERROR:', err);
})
})
return "Running Jobs"
}
function asyncQuery(sqlQuery) {
const options = {
query: sqlQuery,
useLegacySql: false,
};
let job;
return bigquery
.createQueryJob(options)
.then(results => {
job = results[0];
console.log(`Job ${job.id} started.`);
return job.promise();
})
.then(() => {
// Get the job's status
return job.getMetadata();
})
.then(metadata => {
// Check the job's status for errors
const errors = metadata[0].status.errors;
if (errors && errors.length > 0) {
throw errors;
}
})
.then(() => {
console.log(`Job ${job.id} completed.`);
return job.getQueryResults();
})
.then(results => {
const rows = results[0];
return rows;
})
.catch(err => {
console.error('ERROR:', err);
});
}
运算符,而不支持aggregation
运算符。因此,您可能应该将$not
$in
与$expr
表达式一起使用
以这种方式
query