我尝试过使用model.find()的不同变体,但没有人做我想做的事。
下面的代码是我正在使用的代码,但它显示每个字段,我只想要“iframe”字段。app.get('/api/videos', function (req, res) {
Video.find({}, function (err, docs) {
res.json(docs);
});
});
下面的代码确实有效,我只获得'iframe'字段,但它会反转json输出,我不希望这样。它也获得了独特的价值,即使它并不重要,因为每个条目都是独一无二的。
app.get('/api/videos', function (req, res) {
Video.find().distinct('iframe', function (err, docs) {
res.json(docs);
});
});
w ^
答案 0 :(得分:3)
您要找的是projection:
Video.find({}, {iframe: 1}, function (err, docs) {
res.json(docs);
});
find
函数的第二个参数告诉返回哪个字段。如果您不想要_id
,请使用:{_id:0, iframe:1}
像这样:
Video.find({}, {_id:0, iframe:1}, function (err, docs) {
res.json(docs);
});
但是,投影不会为您提供不同的值。它只返回您想要使用的字段(以及重复)。
答案 1 :(得分:1)
您可以使用 void Start() {
waveActive = false;
dropheight = 100;
}
// Update is called once per frame
void Update()
{
if(waveActive == false)
{
StartCoroutine(SelectWave());
}
}
IEnumerator SelectWave()
{
waveActive = true;
float val = Random.value;
if(val <= .25)
{
InvokeRepeating("Wave1", delay, delay);
}
if (val > .25 && val <= .50)
{
InvokeRepeating("Wave2", delay, delay);
}
if (val > .5 && val <= .75)
{
InvokeRepeating("Wave3", delay, delay);
}
if (val > .75)
{
InvokeRepeating("Wave4", delay, delay);
}
yield return new WaitForSeconds(Random.Range(20f,30f));
CancelInvoke();
waveActive = false;
}
void Wave1()
{
Debug.Log("Wave1");
delay = .5f;
Instantiate(smallFlame, new Vector3(drop1, dropHeight, 0), Quaternion.identity);
Instantiate(smallFlame, new Vector3(drop2, dropHeight, 0), Quaternion.identity);
Instantiate(smallFlame, new Vector3(drop3, dropHeight, 0), Quaternion.identity);
}
void Wave2()
{
Debug.Log("Wave2");
delay = 1f;
Instantiate(largeFlame, new Vector3(drop1, dropHeight, 0), Quaternion.identity);
Instantiate(largeFlame, new Vector3(drop2, dropHeight, 0), Quaternion.identity);
Instantiate(largeFlame, new Vector3(drop3, dropHeight, 0), Quaternion.identity);
}
void Wave3()
{
Debug.Log("Wave3");
delay = 4f;
Instantiate(mediumFlame, new Vector3(drop1, dropHeight, 0), Quaternion.identity);
Instantiate(mediumFlame, new Vector3(drop2, dropHeight, 0), Quaternion.identity);
Instantiate(mediumFlame, new Vector3(drop3, dropHeight, 0), Quaternion.identity);
}
void Wave4()
{
Debug.Log("Wave4");
delay = .5f;
Instantiate(smallFlame, new Vector3(drop1 - dropChange, dropHeight + dropChange, 0), Quaternion.identity);
Instantiate(smallFlame, new Vector3(drop1, dropHeight, 0), Quaternion.identity);
Instantiate(smallFlame, new Vector3(drop1 + dropChange, dropHeight + dropChange, 0), Quaternion.identity);
Instantiate(smallFlame, new Vector3(drop2 - dropChange, dropHeight + dropChange, 0), Quaternion.identity);
Instantiate(smallFlame, new Vector3(drop2, dropHeight, 0), Quaternion.identity);
Instantiate(smallFlame, new Vector3(drop2 + dropChange, dropHeight + dropChange, 0), Quaternion.identity);
Instantiate(smallFlame, new Vector3(drop3 - dropChange, dropHeight + dropChange, 0), Quaternion.identity);
Instantiate(smallFlame, new Vector3(drop3, dropHeight, 0), Quaternion.identity);
Instantiate(smallFlame, new Vector3(drop3 + dropChange, dropHeight + dropChange, 0), Quaternion.identity);
}
参数指定要包含在结果中的字段。
db.collection.find(查询,投影)
如果find()包含投影参数,则匹配文档仅包含投影字段和_id字段。您可以选择排除_id字段。
投影参数采用以下形式的文档:
projection
{ field1: <boolean>, field2: <boolean> ... }
值可以是以下任何一种:1或true表示包含该字段。 find()方法始终包含_id字段,即使未明确声明该字段在投影参数中返回。
0或false以排除该字段。
如果您包含字段,则只能在<boolean>
字段的情况下明确排除字段。
_id