1
基於this question和this issuefind
是比findOne
更快,如果查詢未執行,所以我試圖在node.js中這樣做。貓鼬發現沒有返回結果時,它mongodb它
在我執行以下命令mongodb的(CLI):
> db.carts.find({_id: ObjectId("57bd3df5c878685d3ec581d6")}).limit(1).count()
1
然後在node.js中,使用貓鼬我使用這個代碼,總是返回0:
const cartId = '57bd3df5c878685d3ec581d6';
const query = Cart.find({ _id: new mongoose.Types.ObjectId(cartId) }).limit(1);
console.log(query.count()); // 0
還注意到,如果查詢被執行,則它返回購物車:
query.exec().then(console.log); // [ { _id: 57bd3df5c878685d3ec581d6, __v: 0, ...
這裏是購物車模型:
const cartSchema = new Schema({
cartItems: [],
cartItemsCount: { type: Number, default: 0 },
cartTotal: {
EUR: {
value: { type: Number, default: 0 },
currencyPrec: { type: Number, default: 2 },
count: { type: Number, default: 0 },
},
},
cartTotalInEur: {
itemsCost: { type: Number, default: 0 },
shippingCost: { type: Number, default: 0 },
discounts: {
quantity: { type: Number, default: 0 },
percent: { type: Number, default: 0 },
couponCode: String,
},
tax: { type: Number, default: 0 },
},
shippingInfo: {
email: String,
receiver: String,
street: String,
postcode: String,
city: String,
country: {
name: String,
'alpha-2': String,
'alpha-3': String,
'sub-region-code': String,
'region-code': String,
'sub-region': String,
region: String,
},
isUrgent: Boolean,
isSaveData: Boolean,
},
created: { type: Date, default: Date.now },
updated: { type: Date, default: Date.now },
});
const Cart = mongoose.model('cart', cartSchema);
我做錯了什麼?
你可以顯示你的車/車的模型聲明?想知道那裏是否有問題。 – Wake
你可以試試'Cart.find({_id:cartId})。limit(1).count();'? –
@ L.Meyer我已經試過了,結果相同(原來是我第一次試用:) – David