1

I have a data collection which contains records in the following format.

{
    "_id": 22,
    "title": "Hibernate in Action",
    "isbn": "193239415X",
    "pageCount": 400,
    "publishedDate": ISODate("2004-08-01T07:00:00Z"),
    "thumbnailUrl": "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/bauer.jpg",
    "shortDescription": "\"2005 Best Java Book!\" -- Java Developer's Journal",
    "longDescription": "Hibernate practically exploded on the Java scene. Why is this open-source tool so popular  Because it automates a tedious task: persisting your Java objects to a relational database. The inevitable mismatch between your object-oriented code and the relational database requires you to write code that maps one to the other. This code is often complex, tedious and costly to develop. Hibernate does the mapping for you.    Not only that, Hibernate makes it easy. Positioned as a layer between your application and your database, Hibernate takes care of loading and saving of objects. Hibernate applications are cheaper, more portable, and more resilient to change. And they perform better than anything you are likely to develop yourself.    Hibernate in Action carefully explains the concepts you need, then gets you going. It builds on a single example to show you how to use Hibernate in practice, how to deal with concurrency and transactions, how to efficiently retrieve objects and use caching.    The authors created Hibernate and they field questions from the Hibernate community every day - they know how to make Hibernate sing. Knowledge and insight seep out of every pore of this book.",
    "status": "PUBLISH",
    "authors": ["Christian Bauer", "Gavin King"],
    "categories": ["Java"]
}

I want to print title, and authors count where the number of authors is greater than 4. I used the following command to extract records which has more than 4 authors.

db.books.find({authors:{$exists:true},$where:'this.authors.length>4'},{_id:0,title:1});

But unable to print the number of authors along with the title. I tried to use the following command too. But it gave only the title list.

db.books.find({authors:{$exists:true},$where:'this.authors.length>4'},{_id:0,title:1,'this.authors.length':1});

Could you please help me to print the number of authors here along with the title?

1 Answer 1

2

You can use aggregation framework's $project with $size to reshape your data and then $match to apply filtering condition:

db.collection.aggregate([
    {
        $project: {
            title: 1,
            authorsCount: { $size: "$authors" }
        }
    },
    {
        $match: {
            authorsCount: { $gt: 4 }
        }
    }
])

Mongo Playground

Sign up to request clarification or add additional context in comments.

1 Comment

Could you please have a look at this too. stackoverflow.com/q/56027966/7234570

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.