2016-07-07 60 views
0
var customerData = Customers.findOne(
    {'phone': phone}, 
    { 'restaurants': { $elemMatch: { 'restaurantId': Meteor.userId() } } } 
); 

我使用$elemMatchRestaurants阵列, 只返回一个对象,但它毕竟是返回array.WHY的所有对象的数组中返回一个对象?如何从对象的MongoDB


是他们只返回一个对象的任何其他方式?

+2

第一个参数是对于查询,第二个是用于投影(https://docs.mongodb.com/manual/reference/method/db。 collection.findOne /#db.collection.findOne)。您的查询应该如下所示:'Customers.findOne({'phone':phone,'restaurants.restaurantId':Meteor.userId()});' – Gemmi

回答

0
var customerData = Customers.findOne( 
{'phone': phone`,'restaurants.restaurantId':Meteor.userId()`}).restaurants; 

它必须是可行的解决方案

由于