Tag: 聚合

聚合中的mongoose模式方法

怎样才能在聚合中访问方法? let mongoose = require("mongoose"), Schema = mongoose.Schema; let UserSchema = new Schema({ name: String, lastname: String }); UserSchema.methods.fullname = () => { return this.name + " " + this.lastname; }; let UserModel = mongoose.model("user",UserSchema); let query = [ { $match: { "name": /foo/i } } ]; UserModel.aggregate(query, (err, users) => { if(err) throw new […]

如何使用mongoose羽毛适配器编写一个聚合体?

我是feathersjs框架的新手,并试图写聚合查询它不能正常工作。 hook.app.query = { lookup: { from: "orders", localField:"serviceLocationId", foreignField:"serviceLocationId", as: "orders" }, match: { serviceLocationId : { $in: Array.from(new Set(reqArr)) } }, limit: 14 } hook.app.service('servicelocations') .find(hook.app.query) .then(result => { console.log(result) })

MongoDB聚合,geNear并迭代callback

我有一个问题,我找不到解决scheme。 我有一些MongoSchemas用于存储来自用户的地理定位。 手机每隔5分钟发送一次经纬度。 这个API是完美的。 Mongo-Schema看起来像: // Importing Node packages required for schema const mongoose = require('mongoose'); const Schema = mongoose.Schema; //= =============================== // User Schema //= =============================== const GeolocationSchema = new Schema({ loc: { type: { type: String }, coordinates: { type: [Number], index: '2dsphere' } }, user: { type: Schema.Types.ObjectId, ref: 'User' } }, […]

如何使用聚合函数来计算mongoDB中的唯一数据

这是input的JSON数据这样存储在mongoDB中 input [{ 'name' : 'user1', 'date' : '23/09/2017', 'deviceType' : 'Android' }, { 'name' : 'user2', 'date' : '24/09/2017', 'deviceType' :'ios' }, { 'name' : 'user1', 'date' : '23/09/2017', 'deviceType' :'ios' }, { 'name' : 'user2', 'date' : '23/09/2017', 'deviceType' :'ios' }, { 'name' : 'user1', 'date' : '24/09/2017', 'deviceType' :'ios' }, { 'name' […]

如何在Mongoose中进行聚合+填充

我感谢一些帮助。 我正在用express和mongodb(v3.4.4),使用mongoose(v4.10.5)做api rest。 我需要做一个聚合操作,但我不处理它。 我给你看一些代码。 模型(它有更多的属性,但我已经离开它简单): const CategoryModel = mongoose.model('Category', new Schema({ slug: { type: String, unique: true, lowercase: true, index: true }, description: String })); const MyModel = mongoose.model('MyModel', new Schema({ category: { type: Schema.Types.ObjectId, ref: 'Category' }, other: [{ type: Schema.Types.ObjectId, ref: 'Other' }], times_count: { type: Number, default: 0 } })); 重要的是,我有兴趣填充MyModel的category字段,而不是other字段。 […]

mongoose组通过查找字段

我是NodeJS和Mongoose的新手,我试图做一些高级查询。 我有三个文件(类别,子类别和产品) Category.js var mongoose = require('mongoose'); var CategorySchema = new mongoose.Schema({ name: { type: String, required: true } }); mongoose.model('Category', CategorySchema); module.exports = mongoose.model('Category'); Subcategory.js var mongoose = require('mongoose'); var SubcategorySchema = new mongoose.Schema({ name: { type: String, required: true }, image: { type: Buffer, contentType: String }, idcategory: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' […]

如何计算mongoDB中唯一数据的数量

我在mongoDB中是非常新的初学者。 我们必须显示输出数据 用户按date的唯一编号。 Android用户和Ios用户的数量。 这是inputJSON数据 input [{ 'name' : 'user1', 'date' : '23/09/2017', 'deviceType' : 'Android' }, { 'name' : 'user2', 'date' : '24/09/2017', 'deviceType' :'ios' }, { 'name' : 'user1', 'date' : '23/09/2017', 'deviceType' :'ios' }, { 'name' : 'user2', 'date' : '23/09/2017', 'deviceType' :'ios' }, { 'name' : 'user1', 'date' : '24/09/2017', 'deviceType' […]

node.js – mongodb native中的RegExp

我使用node-mongodb-native来实现MongoDB查询function。 我需要执行一个查询,如果tag匹配的一部分或组合firstName和lastName像这样: filter50WithTagSkip(tag, skip) { return new Promise((resolve, reject) => { const regex = new RegExp(tag, 'g'); const or_criteria = [ { firstName: { $regex: regex, $options: 'i' } }, { lastName: { $regex: regex, $options: 'i' } }, { fullName: { $regex: regex, $options: 'i' } }, { email: { $regex: regex, $options: 'i' […]

Elasticsearch聚合部分string,而不是全部string

基本上,我在这里要做的是从层级存储的string中获得二级下来的类别。 问题在于层次的层次有所不同,一个产品类别可能有六个层次,另一个只有四个层次,否则我会实施预定义的层次。 我有一些类别如下的产品: [ { title: 'product one', categories: [ 'clothing/mens/shoes/boots/steel-toe' ] }, { title: 'product two', categories: [ 'clothing/womens/tops/sweaters/open-neck' ] }, { title: 'product three', categories: [ 'clothing/kids/shoes/sneakers/light-up' ] }, { title: 'product etc.', categories: [ 'clothing/baby/bibs/super-hero' ] }, … more products ] 我试图得到像这样的聚合桶: buckets: [ { key: 'clothing/mens', … }, { key: 'clothing/womens', […]