当用户创建注释时,Mongoose API会创建一个文档,其中包含用户注释的值,然后仅使用默认参数创建其他文档。有没有办法从数据库中删除它?
我的Note架构是这样的:
var NoteSchema = new mongoose.Schema({
title: String,
text: String,
color: {
type: String,
default: "white", <--- default
}
favorited: {
type: Boolean,
default: false, <--- default
}
});
在mongo
控制台中:
// Document it creates and what I expect
{
"_id": "58795af461e2db2db804997d",
"title" : "Testing: Hello",
"favorited" : false,
"color": "strawberry",
"author": {
"id": "587950df61e2db2db8049972",
"username" : "tester"
}
}
// Additional default (unneccessary) document created:
{
"_id": "58795af461e2db2db804997e",
"favorited": false, <--- from the Schema
"color": "gray" <--- from the Schema
}
从用户的角度来看,它没有做任何事情,但我不希望它为每个音符创建一个包含默认值的附加文档。我怎么能摆脱它?
答案 0 :(得分:1)
您可以更改架构,为#include <iostream>
#include <string>
#include <vector>
using namespace std;
class my_pair
{
private:
int m_x,m_y;
public:
my_pair( int x, int y ) : m_x( x ), m_y( y ) {}
void swap()
{
int tmp = m_x;
m_x = m_y;
m_y = tmp;
}
int x()
{
return m_x;
}
int y()
{
return m_y;
}
};
int main()
{
// Generate some input:
// I'm assuming that the tokens in the columns are
// in the form 0,1,2 etc such that the tokens
// themselves can be interpreted as an index of an array.
// I think there is no loss of generality here, as it can
// be considered a re-labeling...
vector<my_pair> pair_vector;
pair_vector.push_back( my_pair( 0,1 ) );
pair_vector.push_back( my_pair( 1,1 ) );
pair_vector.push_back( my_pair( 2,2 ) );
pair_vector.push_back( my_pair( 2,1 ) );
pair_vector.push_back( my_pair( 2,0 ) );
pair_vector.push_back( my_pair( 2,1 ) );
pair_vector.push_back( my_pair( 2,1 ) );
pair_vector.push_back( my_pair( 2,2 ) );
cout << "Start vector:" << endl;
for( auto p : pair_vector )
cout << p.x() << ' ' << p.y() << endl;
// 1: Count number of occurrences of each token.
// Here the assumption on the tokens is used:
vector<int> num_tokens;
for( auto& p : pair_vector )
{
if( p.x()+1 > num_tokens.size() )
num_tokens.resize( p.x()+1 );
num_tokens[p.x()]++;
if( p.y()+1 > num_tokens.size() )
num_tokens.resize( p.y()+1 );
num_tokens[p.y()]++;
}
// 2. Now I iterate through the columns for each token
// and swap each token
// that appears more often than n/2 times in that column,
// where n is the number of appearances of each token:
bool swap_was_performed = true;
while( swap_was_performed )
{
int token = 0;
swap_was_performed = false;
for( auto& n : num_tokens ) // n is the number (or frequency) of tokens
{
int x_ctr = 0;
int y_ctr = 0;
// Iterating through the input columns:
for( auto& p : pair_vector )
{
if( p.x()==token ) // x-values (i.e. the first column)
{
x_ctr++;
if( x_ctr>n/2 )
{
p.swap();
swap_was_performed = true;
}
}
}
for( auto& p : pair_vector )
{
if( p.y()==token ) // y-values...
{
y_ctr++;
if( y_ctr>n/2 )
{
p.swap();
swap_was_performed = true;
}
}
}
token++;
}
}
cout << "After re-arranging:" << endl;
for( auto p : pair_vector )
cout << p.x() << ' ' << p.y() << endl;
}
和required
字段设置minlength
和title
验证。
text
这将确保如果缺少var NoteSchema = new mongoose.Schema({
title: {
type: String,
required: true,
minlength: 15 // <--- Set the minimum title length
},
text: {
type: String,
required: true,
minlength: 100 // <--- Set the minimum text length
},
color: {
type: String,
default: "white",
}
favorited: {
type: Boolean,
default: false,
}
});
和/或title
,则不会保存文档。此外,如果长度小于text
值,则不会保存文档。