查询网上的手册,都是一些简单的例子,不懂怎样写才能达到下面的需求。
模型是这样的
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中的数据。
PHPz2017-04-17 15:45:33
db.data.find({"push":1,"msgid":{"$regex":"[.-222|222-.] ","$options": "i"}},{"val":1}).sort({"time":1})
$regex는 정규 일치로 작성할 수 있는 정규 표현식입니다. "$options":"i"는 대소문자를 구분하지 않습니다.
find의 두 번째 매개변수는 반환해야 하는 필드입니다. , 0은 반환하지 않음을 의미합니다
sort {"time":1} 1은 시간에 따라 양의 순서이고, -1은 시간에 따라 역순입니다