How to refer to each property of an object in an array of objects in MongoDB MapReduce JavaScript query?

CommaToast

How to refer to each property of an object in an array of objects in MongoDB MapReduce JavaScript query?

Here is my data:

{
  "_id": ObjectId("544ae3de7a6025f0470041a7"),
  "name": "Bundle 4",
  "product_groups": [
    {
      "name": "camera group",
      "products": [
        {
          "$ref": "products",
          "$id": ObjectId("531a2fcd26718dbd3200002a"),
          "$db": "thisDB"
        },
        {
          "$ref": "products",
          "$id": ObjectId("538baf7c26718d0a55000043"),
          "$db": "thisDB"
        },
        {
          "$ref": "products",
          "$id": ObjectId("538baf7c26718d0a55000045"),
          "$db": "thisDB"
        }
      ]
    },
    {
      "name": "lens group",
      "products": [
        {
          "$ref": "products",
          "$id": ObjectId("531e3ce926718d0d45000112"),
          "$db": "thisDB"
        },
        {
          "$ref": "products",
          "$id": ObjectId("531e3ce926718d0d45000113"),
          "$db": "thisDB"
        }
      ]
    }
  ]
}

Here is my map function: (for simplicity I took out the reduce option since it doesn't matter if the map doesn't work right)

var map = function() { emit(this.product_groups, this.product_groups.products); };
db.instant_rebates.mapReduce(
    map,
    {
        out: "map_reduce_example",
        query: {"_id": ObjectId("544ae3de7a6025f0470041a7")}
    }
);

However the problem is that the "value" field in the result always comes up as "undefined". Why? Why doesn't this.product_groups.products return the products array? How do I fix this?

Also, I want it to do is to emit TWICE, once for each of the two product_groups. But so far it only emits ONCE. How do I fix that?

Neil Lunn

Under mapReduce operations the documents are presented as JavaScript objects so you need to treat them as such and traverse them. That means processing each member of the array:

var map = function() { 
    this.product_groups.forEach(function(group) {
        emit( group.name, { products: group.products } );
    });
};
var reduce = function(){};

db.instant_rebates.mapReduce(
    map,
    reduce,
    {
        out: "map_reduce_example",
        query: {"_id": ObjectId("544ae3de7a6025f0470041a7")}
    }
);

The requirements of the "emit" function is both a "key" and a "value" to be presented as arguments which are emitted. The "value" must be singular therefore to emit an "array" of data you need to wrap this under the property of an object. The "key" must be a singular value as it's intent to it be used as the "grouping key" in the reduce operation, and the "name" field should be sufficient at least for example.

Naturally since there is a top level array in the document you process "each element" as is done with the function, and then each result is "emitted" so there are "two" results emitted from this one document.

You also need to at least define a "reduce" function even if it never gets called because all of the emitted keys are different, as is the case here.

So, it's JavaScript. Treat a list structure as a list.

Please note. This is all your question is about. If you want to ask further questions on mapReduce then please ask other questions. Don't ask any more of this one. I don't want to talk about your field naming, or go into detail of how this seems to be working towards "how do I pull in data from the other collection", which is something you cannot do.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

MongoDB C# Query Array of Objects that contain a property value

분류에서Dev

How to flatten an array of objects into arrays for each object parameter?

분류에서Dev

JavaScript--merge two array objects into one arr object then loop through each index/object

분류에서Dev

query to retrieve multiple objects in an array in mongodb

분류에서Dev

filter an array of objects based on an object with an array as it's property value in react

분류에서Dev

Inherited child objects share the same array property in JavaScript?

분류에서Dev

How to access attributes of php object inside a array of php objects from javascript

분류에서Dev

How to access JavaScript object other members data whilst iterating over array in handlebars each statement

분류에서Dev

how is Array object different from other objects

분류에서Dev

How to update document in an object in an array in an object in an array using MongoDB?

분류에서Dev

How to compare two object elements in a mongodb array

분류에서Dev

Search MongoDB Array of Objects where property has same value for more than 1 array element

분류에서Dev

php array of objects can't get object property (Object of class stdClass could not be converted to string)

분류에서Dev

How to display same property from different objects in Javascript

분류에서Dev

How to combined array of objects one to another array using javascript

분류에서Dev

How can I retrieve the object into an array in Javascript?

분류에서Dev

Javascript array of objects undefined

분류에서Dev

How to remove a property from a JavaScript object when using Strict Mode

분류에서Dev

MongoDB - Query on key name of an object

분류에서Dev

Changing object at NSMutable Array of Objects

분류에서Dev

Populating Object array with new Objects

분류에서Dev

How to click through array of objects and display data in div using javascript

분류에서Dev

Iterate over each object In array

분류에서Dev

Get Object Property Value in JavaScript

분류에서Dev

Javascript object property: string or int?

분류에서Dev

Reduce Array to objects that share certain property: values

분류에서Dev

Keep getting [object, Object] from array for $.each

분류에서Dev

How to access a property in array of objects without looping through all of them in php?

분류에서Dev

Adding OnClick property to array javascript

Related 관련 기사

  1. 1

    MongoDB C# Query Array of Objects that contain a property value

  2. 2

    How to flatten an array of objects into arrays for each object parameter?

  3. 3

    JavaScript--merge two array objects into one arr object then loop through each index/object

  4. 4

    query to retrieve multiple objects in an array in mongodb

  5. 5

    filter an array of objects based on an object with an array as it's property value in react

  6. 6

    Inherited child objects share the same array property in JavaScript?

  7. 7

    How to access attributes of php object inside a array of php objects from javascript

  8. 8

    How to access JavaScript object other members data whilst iterating over array in handlebars each statement

  9. 9

    how is Array object different from other objects

  10. 10

    How to update document in an object in an array in an object in an array using MongoDB?

  11. 11

    How to compare two object elements in a mongodb array

  12. 12

    Search MongoDB Array of Objects where property has same value for more than 1 array element

  13. 13

    php array of objects can't get object property (Object of class stdClass could not be converted to string)

  14. 14

    How to display same property from different objects in Javascript

  15. 15

    How to combined array of objects one to another array using javascript

  16. 16

    How can I retrieve the object into an array in Javascript?

  17. 17

    Javascript array of objects undefined

  18. 18

    How to remove a property from a JavaScript object when using Strict Mode

  19. 19

    MongoDB - Query on key name of an object

  20. 20

    Changing object at NSMutable Array of Objects

  21. 21

    Populating Object array with new Objects

  22. 22

    How to click through array of objects and display data in div using javascript

  23. 23

    Iterate over each object In array

  24. 24

    Get Object Property Value in JavaScript

  25. 25

    Javascript object property: string or int?

  26. 26

    Reduce Array to objects that share certain property: values

  27. 27

    Keep getting [object, Object] from array for $.each

  28. 28

    How to access a property in array of objects without looping through all of them in php?

  29. 29

    Adding OnClick property to array javascript

뜨겁다태그

보관