我有用户的集合,我想从中删除只有2个字段的文档.我的一般架构是这样的:
{
_id: 1,
name: af,
city: asd,
transaction: 1,
transactions:[{
id:1,
product: mobile,
amount: 10
},
id:2,
product: tv,
amount: 23
}],
many-other-sub-docs:[],
}
Run Code Online (Sandbox Code Playgroud)
我想删除只有_id&transactionfield存在而不存在其他文档的文档.
注意:我有大约30-40个字段.
删除这些文档的一种方法是指定查询中不应存在的所有字段以及仅存在应存在的字段.例如db.users.remove({_id:{$exists:true}, transaction:{$exists:true}, other_field1:{$exists:false}, other_field2:{$exists:false}, ...})
但我觉得这个查询很荒谬.此外,我必须找到我的收藏中的所有字段.
还有其他更简单的方法吗?
我试图搜索这个问题,但找不到令人满意的答案.所以这是我的问题:
我正在浏览具有以下代码变体的目录:
一世.
#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<sys/stat.h>
void traverseDirectory(char name[100]){
DIR* dir;
struct dirent *ent;
struct stat states;
dir = opendir(name);
while((ent=readdir(dir)) != NULL){
stat(ent->d_name,&states);
if(!strcmp(".", ent->d_name) || !strcmp("..", ent->d_name)){
continue;
}
else{
printf("%s/%s\n",name,ent->d_name);
if(S_ISDIR(states.st_mode)){
strcat(name,"/");
strcat(name,ent->d_name);
traverseDirectory(name);
}
}
}
closedir(dir);
}
int main(){
char path[100];
printf("Enter the path:\n");
scanf("%s",&path);
traverseDirectory(path);
}
Run Code Online (Sandbox Code Playgroud)
这个遍历子目录但在遍历第一个子目录并打印其文件后给出分段错误.
输出是:
Enter the path:
/home/harshad/dump
/home/harshad/dump/TraverseDirectory.c
/home/harshad/dump/temp
/home/harshad/dump/temp/temptest.txt
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
II.
#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<sys/stat.h>
void traverseDirectory(char name[100]){
DIR* dir;
struct dirent *ent;
struct …Run Code Online (Sandbox Code Playgroud)