node.js - mongoose如何同时查询多个字段的多种值
PHPz
PHPz 2017-04-17 15:43:33
0
1
669

查询网上的手册,都是一些简单的例子,不懂怎样写才能达到下面的需求。

模型是这样的

  msgid: { type: 'string' }, //消息ID
  time: { type: 'string' },
  push: { type: 'number'}, //是否推送消息 1是 0否
  val: {
    id: { type: 'string' },
    username: { type: 'string' },
    avatar: { type: 'string' }, 
    content: { type: 'string' },
    type: { type: 'string', default: 'friend' },
    toid: { type: 'string' },
    toname: { type: 'string' }
  }

msgid的值可能是 123-222 , 222-456 这样的。 “-” 两头是两个用户的id值
我想查询
push为1的值 同时 msgid 中包含222的id,无论在前在后。
按time排序。
只返回val中的数据。

PHPz
PHPz

学习是最好的投资!

reply all(1)
PHPzhong

db.data.find({"push":1,"msgid":{"$regex":"[.-222|222-.]+","$options":"i"}}, {"val":1}).sort({"time":1})

$regex is a regular expression that can be written in regular matching. "$options":"i" is not case-sensitive.
The second parameter of find is the field that needs to be returned. 1 means return, 0 means not to return.
sort {"time ":1} 1 is in positive order by time, -1 is in reverse order by time

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template