我有一个mongo架构,如下所示:
{
"_id" : ObjectId("58e4222497b2735ba3cd9ec4"),
"place" : "",
"plant" : "Test1",
"eventDate" : ISODate("2017-04-05T00:00:00Z"),
"event" : "Test123",
"toBeTested" : [
{
"_id" : ObjectId("58e453a07c9f94702ebac93d"),
"thingsTested" : [
"A1",
"A2",
"A3"
]
}
]}
我正在使用mongoose删除thingsTested
的单个元素。我在mongoose中的代码是:
Layout
.update(
{_id: req.params.parentid},
{$pull: {toBeTested: {thingsTested: 'A3'}}},
function (err, docs){
if (err) {
res.send(err);
}
res.json(docs);
}
);
正如您所看到的,我已经硬编码我要从A3
集中移除thingsTested
。但是,展示的行为是所有thingsTested
都被删除。
作为后续问题,如何确保mongoose命令仅使用A3
thingsTested
(子ID)删除_id
中的58e453a07c9f94702ebac93d
?< / p>
谢谢!
答案 0 :(得分:1)
您将不得不在查询中选择外部数组元素,然后可以使用#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <ctime>
using namespace std;
struct information{
string first, middle, last, contribution;
int birthyear;
};
int main()
{
int i;
information employee[1000];
ifstream fin;
ofstream fout;
string input;
int comma; //stores the location of the comma
int space, space2, space3; //store the location of the spaces.
int dollarsign; //checks where the dollar sign is and then the numbers after.
fin.open("oldretirement.txt"); //opening input..
if (fin.fail()){
fout<<"\nError! failed to open file!";
exit(1);
}
fout.open("newretirement.txt");
if (fout.fail()){
fout<<"\nError! failed to open file!";
exit(1);
}
while(!fin.eof()){ //loops till file failure
getline(fin, input);
comma = input.find(",");
if (isalpha(input[comma-1])){
space = input.find(" "); //finds first space
employee[i].last = input.substr(0, space-2);
space2 = input.find(" ", space + 1); //finds the next space
employee[i].first = input.substr(space+1, space2-space); //gets the substring of the area between spaces
space3 = input.find(" ", space2+1); //you get the idea
employee[i].middle = input.substr(space2+1, space3-space2);
employee[i].birthyear = atoi((input.substr(space3+1, 4)).c_str());
employee[i].contribution = input.substr(space3+5);
}
else{
space = input.find(" ");
employee[i].first = input.substr(0, space-1);
space2 = input.find(" ", space+1);
employee[i].middle = input.substr(space+1, space2-space);
space3 = input.find(" ", space2+1);
employee[i].last = input.substr(space2+1, space3-space2);
employee[i].birthyear = atoi((input.substr(space3, 4)).c_str());
dollarsign = input.find("$");
employee[i].contribution = input.substr(dollarsign+2);
}
i++;
}
i--; //makes sure to make the number of indexes not too many.
int money_made; //something to save how much money they made the company other than a string, so that it is easier to compare.
string conversion; //something to store a string into to make things easier.
for (int j= 0;j<i;j++){ //looping for printing stuff
fout<<employee[j].last<<", "<<employee[j].first<<" "<<employee[j].last<<" "<<employee[j].birthyear<<employee[j].contribution;
conversion = employee[j].contribution;
conversion.erase((conversion.find(","), 1)); //erases the comma to make conversion to int easier.
money_made = atoi(conversion.c_str()); //converts string to integer
int age = (2017 - employee[j].birthyear);
if( (money_made > 300000)&&(age >= 60))
fout<<" Ready\n"; //
else
fout<<endl;
}
fin.close();
fout.close();
}
引用匹配的元素,如下所示:
$