0

My data look like this:

{'_id': ObjectId('6068da8878fa2e568c42c7f1'),
 'first': datetime.datetime(2018, 1, 24, 14, 5),
 'last': datetime.datetime(2018, 1, 24, 15, 5),
 'maxid13': 12.5,
 'minid13': 7.5,
 'nsamples': 13,
 'samples': [{'c14': 'C',
              'id1': 3758.0,
              'id10': 0.0,
              'id11': 274.0,
              'id12': 0.0,
              'id13': 7.5,
              'id15': 0.0,
              'id16': 73.0,
              'id17': 0.0,
              'id18': 0.342,
              'id19': 6.3,
              'id20': 1206.0,
              'id21': 0.0,
              'id22': 0.87,
              'id23': 0.0,
              'id6': 2.0,
              'id7': -79.09,
              'id8': 35.97,
              'id9': 5.8,
              'timestamp1': datetime.datetime(2018, 1, 24, 14, 5),
              'timestamp2': datetime.datetime(2018, 1, 24, 9, 5)},
             {'c14': 'C',
              'id1': 3758.0,
              'id10': 0.0,
              'id11': 288.0,
              'id12': 0.0,
              'id13': 8.4,
              'id15': 0.0,
              'id16': 71.0,
              'id17': 0.0,
              'id18': 0.342,
              'id19': 6.3,
              'id20': 1207.0,
              'id21': 0.0,
              'id22': 0.69,
              'id23': 0.0,
              'id6': 2.0,
              'id7': -79.09,
              'id8': 35.97,
              'id9': 6.2,
              'timestamp1': datetime.datetime(2018, 1, 24, 14, 10),
              'timestamp2': datetime.datetime(2018, 1, 24, 9, 10)},
               .
               .
               .
               .

Can someone help on how to find for example the id13 when timestamp1 is equals to datetime.datetime(2018, 1, 24, 14, 5) Samples is an array. This is what i have wrote.

cursor = mydb1.mongodbbucket.aggregate(
          [
              {
                "$match": {
                   "samples.timestamp1": {"$eq": datetime.strptime("2018-01-24 14:10:00", "%Y-%m-%d %H:%M:%S")}
                }
              },

              {
                  "$project": {

                      "samples.id13": 1
                  }
              },
            ]
        )   )

The ideal output would be id13:7.5

2 Answers 2

1

Try this:

cursor = mydb1.mongodbbucket.aggregate([
    { "$unwind": "$samples" },
    {
        "$match": {
            "samples.timestamp1": { "$eq": datetime.strptime("2018-01-24 14:10:00", "%Y-%m-%d %H:%M:%S") }
        }
    },
    {
        "$project": {
            "samples.id13": 1
        }
    }
])
Sign up to request clarification or add additional context in comments.

8 Comments

Code is in JavaScript. You might have to convert it to Python.
Thank you for you help!I appreciate it!I did what you said to me but it doesn't seems to work
What is the output? Are you getting any error?
i get something like this {'_id': ObjectId('6068a09578fa2e568c35bb6a'), 'samples': None} a multiple times
Yes its the same as before unfortunately
|
0

This is the right answer from @dheemanthbhat:

cursor = mydb1.mongodbbucket.aggregate([
    { "$unwind": "$samples" },
    {
        "$match": {
            "samples.id13": { "$exists": true },
            "samples.timestamp1": { "$eq": datetime.strptime("2018-01-24 14:10:00", "%Y-%m-%d %H:%M:%S") }
        }
    },
    {
        "$project": {
            "samples.id13": 1
        }
    }
])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.