Skip to content

Can't query sub-documents in an array #532

Open
@echo66

Description

@echo66

Description

I was trying to query sub-documents in an array. This is the code I'm running:

var jsData        = require('js-data');
var MemoryAdapter = require('js-data-memory');
jsData.utils.Promise = require('bluebird');


var adapter = new MemoryAdapter();

var container = new jsData.Container({ mapperDefaults: { } });

container.registerAdapter('memory', adapter, { 'default': true });



var LangStringSchema = new jsData.Schema({
	type: 'object', 
	properties: {
		lang: { type: 'string' }, 
		value: { type: 'string' }
	}, 
	required: ['lang', 'value']
});


var FoodCategorySchema = new jsData.Schema({
	type: 'object', 
	properties: {
		names: {
			type: 'array', 
			items: LangStringSchema
		}, 
		parentID: { type: 'string' }, 
	}
});


container.defineMapper('food-categories', {
	schema: FoodCategorySchema, 
	relations: {
		belongsTo: {
			'food-categories': {
				foreignKey: 'parentID', 
				localField: 'parentCategory'
			}
		}, 
		hasMany: {
			'food-categories': {
				foreignKey: 'id', 
				localField: 'childrenCategories'
			}
		}
	}
});


var c1 = container.create('food-categories', { 
	names: [
		{ value: 'Carne', lang: 'pt' }, 
		{ value: 'Meat', lang: 'en' }
	]
});

c1.then(function(cat) {
	container.create('food-categories', {
		names: [
			{ value: 'Frango', lang: 'pt' }, 
			{ value: 'Chicken', lang: 'en' }, 
		], 
		parentID: cat['id']
	});

	container.create('food-categories', {
		names: [
			{ value: 'Vaca', lang: 'pt' }, 
			{ value: 'Cow', lang: 'en' }
		], 
		parentID: cat['id']
	});

	container.create('food-categories', {
		names: [
			{ value: 'Porco', lang: 'pt' }, 
			{ value: 'Pork', lang: 'en' }
		], 
		parentID: cat['id']
	});
});


setTimeout(function() {
	container.findAll('food-categories', {
		where: {
			'names.value': {
				'==': 'Cow'
			}, 
			'names.lang': {
				'==': 'en'
			}
		}
	}).then(function(cats) {
		console.log(cats);
	});
}, 200);

I was expecting the output to be an array containing a single document. But, instead, I got an empty array.

Environment

js-data version:

  • js-data@3.0.5
  • js-data-memory@0.1.4

node or browser version:

  • node.js: v8.10.0

operating system:

  • Ubuntu 18.04 LTS

Steps to reproduce

Use the following package.json:

{
  "name": "test-jsdata",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bluebird": "^3.5.3",
    "js-data": "^3.0.5",
    "js-data-memory": "^0.1.4",
    "js-data-rethinkdb": "^3.0.0"
  }
}

The content of index.js is the code snippet I included in the description section.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy